iOS - Cancel constraints animation

1.5k views Asked by At

I'm animating constraints the usual way:

centerXsettingsBtnConstraint.constant = aValue;

[UIView animateWithDuration:.5
                 animations:^{

                      [self.view layoutIfNeeded];

               } completion:^(BOOL finished) {
                      isAnimating = NO;}];

When I'm rotating the device I want to cancel this animation and update constraint with new values.

I tried to call

[self.view.layer removeAllAnimations];

but the animation still goes on till the rotation of the device is over. Only then I can update with new constraints, but during the rotation it's all a mess on the screen.

Is there a way to cancel constraints animation?

3

There are 3 answers

1
lastcc On

You must call the remove.. method on each subview:

[self.view.layer removeAllAnimations];

I'll use Swift:

for eachView in self.view.subviews {
    eachView.layer.removeAllAnimations()
}
0
Mario Jaramillo On

For Swift: This will work to stop the animation of the constraint

yourComponent.layer.removeAnimation(forKey: "position")
0
Neha Tripathi On

You need to remove animation on the specified view whose constraint or property is being changed in that particular animation.

Call removeAllAnimations() on the view on whom centerXsettingsBtnConstraint is applied.

You do not need to call removeAllAnimations() on each subview.

If you are not able to find the particular view, try to print someView.layer.animationKeys(), you will find the view on which animation is applied.