I want to execute doSomethingElse after the animation is finished. Another constraint is that the animation code can be of different duration. How can I do that? thanks!
-(void) doAnimationThenSomethingElse {
[self doAnimation];
[self doSomethingElse];
}
This for example doesn't work:
animationDuration = 1;
[UIView animateWithDuration:animationDuration
animations:^{
[self doAnimation];
} completion:^(BOOL finished) {
[self doSomethingElse];
}
];
When you are not the author of the animation, you can get a callback when the animation ends by using a transaction completion block:
From your comments I'd recommend you:
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html
there is the UIView documentation, scroll down to the
Animating Views with Blocks
and
Animating Views
to see hyperlinks that jump you to different parts of the page that explain how to animate views, the ones that you want are under "Animating Views with Blocks" reading the names of the methods makes them self explanatory
Use block animations:
You need to be able to access the specific instances of the animations you're running in order to coordinate completion actions for each one. In your example, [self doAnimation] doesn't expose us to any animations, so your question cannot be "solved" with what you've provided.
There are few ways to achieve what you want, but it depends on what kind of animations you're dealing with.
As pointed out in other answers, the most common way to execute code after an animation on a view is to pass a completionBlock in:
animateWithDuration:completion:
Another way to handle property change animations is to set aCATransaction
completion block within the scope of the transaction.However, these particular methods are basically for animating property or hierarchy changes to views. It's the recommended way when your animations involve views and their properties, but it doesn't cover all the kinds of animations you might otherwise find in iOS. From your question, it's not clear as to what kind of animations you're using (or how, or why), but if you are actually touching instances of CAAnimations (a key frame animation or a group of animations), what you'll typically do is set a delegate:
The point is, how your completion handling is implemented depends on how your animation is implemented. In this case we can't see the latter, so we can't determine the former.