views:

360

answers:

2

I have an animation which I kick off like this:

[UIView beginAnimations:@"doThis" context:self];
[UIView setAnimationDuration:1.5f];
[UIView setAnimationDelay:2.5f];

Now, the problem is that this animation is told to start in 2.5 seconds. But in the meantime, something may happen and I don't want the animation anymore. However, CA will just animate that thing after 2.5 seconds, no matter what happens. How could I say "no, thank you, don't animate"?

I have other animations going on in different context and animationID, so I don't want to just remove all animations from the app. What's the most clean way of achieving this? Just running another nonsense-animation with same context and animationID and old targets?

+2  A: 

There is another related question here:

http://stackoverflow.com/questions/554997/cancel-a-uiview-animation

One other thing you could do is to put your animation code in a method and then use:

      [self performSelector:@selector(animateMethod) withObject:nil afterDelay:2.5];

Then you can add a check in the beginning of "animateMethod" to see if the animation should still be performed. This however does not help if you want to cancel the animation while it is running.

Kobski
+2  A: 

If you re-set the properties that are currently animating, that should kill the animation. If you have an animation delegate/didStopSelector set, the method will be called with kCFBooleanFalse as the "finished" parameter in this case.

Kevin Ballard
You mean setting the properties to the original state before the animation took place, or to the current visible state (from the presentation tree)? can you specify "re-set the properties" a little more?
HelloMoon