Daphyduck
Daphyduck

Reputation: 127

Calcul height of uiview after animating this one

I have an animation who modify the constraint of an UIView and i need to know the size of this UIView after animate this, but before the animation begin...

storyboard

When the user scroll the UITableView I update the heightConstraint of the black UIView, I need the height after update the UIView because i need in the function viewDidLayoutSubviews to fixe the height of the Yellow UIview.

this is the code I use for animate the UIView:

self.headerIsCollapsed = true
self.heightProfilView.constant -= (self.view.bounds.height / 5)
UIView.animate(withDuration: 0.3, animations: {
   self.view.layoutIfNeeded()
   self.imageUserProfil.layer.opacity = 0
})

and i need the value here :

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    heightTopBar.constant = // here i need the height of the UIView after animating this
}

So, the question is: How I can pre-calculate the height of the UIView after animating this ?

Upvotes: 0

Views: 229

Answers (2)

Y_Y
Y_Y

Reputation: 341

Try this:

self.headerIsCollapsed = true
//create this property at instance level in your viewcontroller class
self.heightBeforeAnimation = self.heightProfileView.frame.size.height
self.heightProfilView.constant -= (self.view.bounds.height / 5)
UIView.animate(withDuration: 0.3, animations: {
  self.view.layoutIfNeeded()
  self.imageUserProfil.layer.opacity = 0
}){ (isCompleted) in
    //create this property at instance level in your viewcontroller class
   self.heightAfterAnimation = self.heightProfileView.frame.size.height
}

and call whatever function and use the value of two variables containing value of height before and after animating your view.

Upvotes: 0

mag_zbc
mag_zbc

Reputation: 6992

Simply use a completion block of UIView.animate()

UIView.animate(withDuration: 0.3, animations: {
    self.view.layoutIfNeeded()
    self.imageUserProfil.layer.opacity = 0
}) { (finished) in
    // get the view's height after animation here
}

Unless you specified duration as 0, the completion block will execute after animation sequence ends.

Upvotes: 0

Related Questions