How to cancel UIView block-based animation? How to cancel UIView block-based animation? ios ios

How to cancel UIView block-based animation?


You can stop all animations on a view by calling:

[view.layer removeAllAnimations];

(You'll need to import the QuartzCore framework to call methods on view.layer).

If you want to stop a specific animation, not all animations, your best best bet is to use CAAnimations explicitly rather than the UIView animation helper methods, then you will have more granular control and can stop animations explicitly by name.

The Apple Core Animation documentation can be found here:

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreAnimation_guide/CreatingBasicAnimations/CreatingBasicAnimations.html


For iOS 10 use UIViewPropertyAnimator to animate. It provides methods to start, stop and pause UIView animations.

 let animator = UIViewPropertyAnimator(duration: 2.0, curve: .easeOut){        self.view.alpha = 0.0 } // Call this to start animation. animator.startAnimation() // Call this to stop animation. animator.stopAnimation(true)


I'd add to Nick's answer that to make removeAllAnimations smooth next idea be very handy.

[view.layer removeAllAnimations];[UIView transitionWithView:self.redView                  duration:1.0f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{                      [view.layer displayIfNeeded];                  } completion:nil];