I've got a few components which I need to animate. I've been using UIVIew.animate()
for a long time with no issues. However in this case, one of the components doesnt animate (specifically pullUpViewBottomCSTR
) , it simply "jumps" the view into place with no animation ( while the rest work as expected ).
Can you offer any possible explanations for this (as it would probably be spammy to copy paste everything). I'm very confused.
UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveEaseOut, animations: {
self.pullUpViewBottomCSTR!.constant = 0
self.mainImagePullUpBottomCSTR.constant = 0
self.obscuringFadeEffectView.alpha = 0;
self.layoutIfNeeded()
}) { (completed) in
//
}
I don't think I've got any conflicting constraints in place.
This code resides in a UIView
component, not a UIViewController
.
Im loading this component inside a viewController. I used to have the whole thing inside the UIViewController and i think it used to work properly until i moved it to its corresponding .xib and .swift files.
Update:
The constraint is originally added as follows. ( this might be relevant )
self.pullUpViewBottomCSTR = parentController.view.bottomAnchor.constraint(equalTo: self.bottomAnchor)
self.pullUpViewBottomCSTR!.isActive = true;
self.pullUpViewBottomCSTR!.constant = 0
I am later using a pan recogniser and update the constraint as needed ( and this is why im keeping a reference to it ).
The view you are tying to animate is self
but its vertical position is controlled by this constraint:
self.pullUpViewBottomCSTR = parentController.view.bottomAnchor.constraint(equalTo: self.bottomAnchor)
When you activate this constraint with:
self.pullUpViewBottomCSTR!.isActive = true
it gets added to the constraints for the earliest common ancestor of self
and parentController.view
(which may be one of those views if one is a direct descendant of the other). In order to animate that constraint, you need to call layoutIfNeeded()
on that earliest common ancestor.
You haven't shown how the view represented by self
is related to parentController.view
, but assuming it is a subview
of that view, then you can animate it with:
self.superview?.layoutIfNeeded()
inside of the animation loop.
Alternatively:
parentController.view.layoutIfNeeded()
should work as well.