io swift Animation: alpha and constraint - ios

This is my code, and the skip button animation does not work. It just disappear immediately.
It just disappear
skipButtonBottomConstraint.constant = -40
UIView.animate(withDuration: 1.0, animations: {
self.titleLabel.alpha = 0
self.skipButton.alpha = 0
self.pageControl.alpha = 0
self.view.setNeedsLayout()
})
could you explain me the reason?

I think Paul is right, and you should be calling layoutIfNeeded() inside the animation block, not setNeedsLayout()

Make sure you are calling layoutIfNeeded after you setNeedsLayout
Also as Apple recommends - call it once before the animation block to ensure that all pending layout operations have been completed.
self.view.layoutIfNeeded()
self.skipButtonBottomConstraint.constant = -40
UIView.animate(withDuration: 1.0, animations: {
self.titleLabel.alpha = 0
self.skipButton.alpha = 0
self.pageControl.alpha = 0
self.view.layoutIfNeeded()
})

Related

Animate Constraint Relative to Another One

I setup an animation to hide one switch/label when another one is turned on. Simultaneously the switch that was just turned on move up. This works great with the simple explanation here.
However, when I try to move the switch/label back down after it is turned off it doesn't budge. The other switch reappears fine, but the top constraint change doesn't fire.
I'm relatively new to doing this type of setup and animating all programmatically and after spending an hour on this I'm stumped. Is it because I'm animating a top constraint relative to another one? How does that matter if it works the first time around? Even though the alpha of the hidden switch is set to zero, its frame is still there, right? Or am I doing something simple stupidly?
// Works Perfectly!
func hideVeg() {
self.view.layoutIfNeeded()
UIView.animate(withDuration: 1, delay: 0, options: [.curveEaseIn], animations: {
self.vegetarianSwitch.alpha = 0
self.vegetarianLabel.alpha = 0
self.veganSwitch.topAnchor.constraint(equalTo: self.vegetarianSwitch.bottomAnchor, constant: -30).isActive = true
self.view.layoutIfNeeded()
})
}
// Showing the label and switch works, but the topAnchor constraint never changes!
func showVeg() {
self.view.layoutIfNeeded()
UIView.animate(withDuration: 1, delay: 0, options: [.curveEaseIn], animations: {
self.vegetarianSwitch.alpha = 1
self.vegetarianLabel.alpha = 1
// This is the constraint that doesn't change.
// This is exactly what it was set to before the other hideVeg() runs.
self.veganSwitch.topAnchor.constraint(equalTo: self.vegetarianSwitch.bottomAnchor, constant: 40).isActive = true
self.view.layoutIfNeeded()
})
}
The issue here is that you are not modifying the constraints but actually creating new constraints with each animation. What you want to do instead is just create the constraint once (you can do it in code or in Interface Builder and drag and outlet). You can then just change the .constant field of the existing constraint in your animation block.
The constant needs to be changed with the animation, not creating an entirely new constraint. The old constraint still exists causing the issue.
var veganTopConstraint = NSLayoutConstraint()
// Top Constraint set up this way so it can be animated later.
veganTopConstraint = veganSwitch.topAnchor.constraint(equalTo: vegetarianSwitch.bottomAnchor, constant: 40)
veganTopConstraint.isActive = true
func hideVeg() {
UIView.animate(withDuration: 1, delay: 0, options: [.curveEaseIn], animations: {
self.vegetarianSwitch.alpha = 0
self.vegetarianLabel.alpha = 0
self.veganTopConstraint.constant = -30
self.view.layoutIfNeeded()
})
}
func showVeg() {
self.view.layoutIfNeeded()
UIView.animate(withDuration: 1, delay: 0, options: [.curveEaseIn], animations: {
self.vegetarianSwitch.alpha = 1
self.vegetarianLabel.alpha = 1
self.veganTopConstraint.constant = 40
self.view.layoutIfNeeded()
})
}

UIStackView Hide Animation not working properly [duplicate]

This question already has answers here:
UIStackView Hide View Animation
(6 answers)
Closed 4 years ago.
I don't know what is going on but this will happened a lot of time to me. I used UIStackView for arranging the view and animation but when I hide the control in UIStackView there will be some sort of weird animation is there. Here I attached video and code.
UIView.animate(withDuration: 0.5, animations: {
self.viewCollectionSpecies.isHidden = true
})
Finally i got clue what is issue. There will be one height constraint which I need to forgot to remove after removing that constraint it work perfectly.
self.heightConstraint.constant = 0.0
UIView.animate(withDuration: 0.5, animations: {
self.viewCollectionSpecies.isHidden = true
self.layoutIfNeeded()
})
Try
self.viewCollectionSpecies.alpha = 0
UIView.animate(withDuration: 0.5, animations: {
self.viewCollectionSpecies.isHidden = true
self.stackView.layoutIfNeeded()
})
Or
self.viewCollectionSpecies.isHidden = true
UIView.animate(withDuration: 0.5, animations: {
self.stackView.layoutIfNeeded()
})
Try working with the alpha value:
func changeViewTo(newView: UIView, oldView: UIView) {
newView.isHidden = false
newView.alpha = 0
UIView.animate(withDuration:0.4, animations: {
oldView.alpha = 0
newView.alpha = 1
}) { (result: Bool) in
oldView.isHidden = true
}
layoutIfNeeded()
self.stackView.layoutIfNeeded()
}

Constraint is not equal to the height

I write this following code :
if !self.headerIsCollapsed{
self.headerIsCollapsed = true
self.heightProfilView.constant -= (self.view.bounds.height / 5)
UIView.animate(withDuration: 0.3, animations: {
self.imageUserProfil.layer.opacity = 0
self.view.layoutIfNeeded()
})
print(self.heightProfilView.constant)
print(self.topUserProfilView.bounds.height)
}
My question is: Why the value of the two print() is not the same ?
I need the value of self.topUserProfilView.bounds.height Before animate this for another function, would there be another way to catch this value before?
thanks
try this: it should work
if !self.headerIsCollapsed{
self.headerIsCollapsed = true
self.heightProfilView.constant -= (self.view.bounds.height / 5)
UIView.animate(withDuration: 0.3, animations: {
self.imageUserProfil.layer.opacity = 0
self.view.layoutIfNeeded()
}) { (isCompleted) in
print(self.heightProfilView.constant)
print(self.topUserProfilView.bounds.height)
}
}
P.S: This solution would not work if you added the heightProfilView constraint relative to the any view height as here you are changing the constant but in the IB you have assigned the multiplier so in that case the default constant value is 0. So, this approach will not work for the relative height constraint. as we can't change the multiplier of a constraint.
Maybe you have constraints taking priority. Check out your console output when you collapse the row, there should be some loud output involving constraints.
Also try hiding imageUserProfil
self.imageUserProfil.layer.opacity = 0
self.imageUserProfil.isHidden = true
You need completion block to do it...Just replace your animation method as below
UIView.animate(withDuration: 0.3, animations: {
self.imageUserProfil.layer.opacity = 0
self.view.layoutIfNeeded()
}) { (isDone) in
print(self.heightProfilView.constant)
print(self.topUserProfilView.bounds.height)
}
Hope it will work.

iOS Animate height constraint issue

I have a problem with the animation of a view after changing it's height constraint. In the screenshot, you can see it's initial value of 120.0.
The animation works but the constraint update from my second view (the blue one) happens directly and not during the animation. This means that second view jumps to the top directly.
With the following code, I will animate the change of the height constraint:
UIView.animate(withDuration: 3.0, animations: {
self.heightConstraint?.constant = 0.0
self.myLabel.alpha = 0.0
self.layoutIfNeeded()
})
Does anybody know why?
self.heightConstraint?.constant = 0.0
self.myLabel.alpha = 0.0
UIView.animate(withDuration: 3.0, animations: {
self.layoutIfNeeded()
})
It should be like this.
For animating constraint changes, you need to write code like below to work.
self.heightConstraint?.constant = 0.0
self.myLabel.alpha = 0.0
UIView.animate(withDuration: 5) {
self.layoutIfNeeded()
}
You need to call self.layoutIfNeeded() before and after updating constraint constant. Change your code to :
self.layoutIfNeeded()
UIView.animate(withDuration: 3.0, animations: {
self.heightConstraint?.constant = 0.0
self.myLabel.alpha = 0.0
self.layoutIfNeeded()
})

Animate view height with Swift

I have view sView inside my ViewController. It height has constraint - I created IBOutlet for this constraint - sViewHeightConstraint. I want to decrease height of sView with animation.
I created function
UIView.animateWithDuration(5.5, animations: {
self.sViewHeightConstraint.constant = 50
})
Height of view is changing but i don't see any animation. What I am doing wrong?
use layoutIfNeeded()
view.layoutIfNeeded() // force any pending operations to finish
UIView.animateWithDuration(0.2, animations: { () -> Void in
self.sViewHeightConstraint.constant = 50
self.view.layoutIfNeeded()
})
swift 3
view.layoutIfNeeded()
sViewHeightConstraint.constant = 50
UIView.animate(withDuration: 1.0, animations: {
self.view.layoutIfNeeded()
})
In case your view updates but animation isn't working, this solution works for me.
UIView.animate(withDuration: 0.2, animations: {
self.sViewHeightConstraint.constant = 50
self.sView.size = CGSize(width: self.sView.frame.width, height: 50)
self.view.layoutIfNeeded()
})

Resources