Cancel a UIView animation?

philsquared picture philsquared · Feb 17, 2009 · Viewed 153.9k times · Source

Is it possible to cancel a UIView animation while it is in progress? Or would I have to drop to the CA level?

i.e. I've done something like this (maybe setting an end animation action too):

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve: UIViewAnimationCurveLinear];
// other animation properties

// set view properties

[UIView commitAnimations];

But before the animation completes and I get the animation ended event, I want to cancel it (cut it short). Is this possible? Googling around finds a few people asking the same question with no answers - and one or two people speculating that it can't be done.

Answer

Jim Heising picture Jim Heising · Mar 19, 2010

Use:

#import <QuartzCore/QuartzCore.h>

.......

[myView.layer removeAllAnimations];