I have multiple animations that have to run as a chain. The way I've been dealing with this is to use a completionHandler and run the next animation block. Is there a cleaner way to deal with this?
[UIView animateWithDuration:1 animations^{
// perform first animation
}completion:(BOOL finished){
[UIView animateWithDuration:1 animations^{
// perform second animation
}completion:(BOOL finished){
}];
}];
You can also use animateWithDuration:delay:options:animations:completion:
with staggered delays so that they start in order, but typically it's best to do them with completion blocks.
If there are several and it's making the code difficult to read, just factor out the blocks (I'm typing this off the top of my head, so it may not compile):
typedef void (^AnimationBlock)();
AnimationBlock firstAnimation = ^{ ... };
AnimationBlock secondAnimation = ^{ ... };
[UIView animateWithDuration:1 animations:firstAnimation completion:(BOOL finished) {
[UIView animateWithDuration:1 animations:secondAnimation];}];
You could create a category on UIView
that took an array of such blocks and chained them together for you, but then you'd have to deal with all the corner cases. How do you define the timings; how do you deal with aborted animations, etc. The above is probably the best approach in most cases.