5

I know you can perform a two-stage animataion using blocks like so:

[UIView animateWithDuration:25.0 delay:0.0 options:UIViewAnimationCurveLinear animations: ^{ aView.alpha = 2.5; } completion:^(BOOL finished) { aView.hidden = YES; } ]; 

..but how would I create a multistage (more than 2) animation using blocks?

1
  • 1
    Multistage animation is really a missing design feature of the UIKit. You can get some of it using QuartzCore, but not much. Someone's ought to come up with a better solution in the coming future. Commented Aug 10, 2011 at 10:26

3 Answers 3

11

Use nested animations:

[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{ //first animation } completion:^(BOOL finished){[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{ //second animation } completion:^(BOOL finished){//and so on.. }];}]; 
Sign up to request clarification or add additional context in comments.

4 Comments

According to Apple's doc, a nested animation block should be in the first animation block itself, not in the completion handler. It's only a naming issue, though. developer.apple.com/library/ios/documentation/windowsviews/…
Actually, Apple also says this is the way to go : Using a completion handler is the primary way that you link multiple animations. They've exampled in Listing 4-2 in their documentation. If you put the nested block in the first block, it'll essentially start simultaneously with the first block, rather than when the animations of the first block have ended.
Absolutely. I wasn't contradicting this point, but only pointing out a naming issue. Using a "completion handler" and a "nested block" are two different techniques, and you said "use nested blocks" while showing a completion handler block. Not a big deal.
I see. By block I had meant a "code segment" rather than an Objective-C block, which caused the confusion. I've edited the answer.
8

or you can make a recursive, multi-stage animation method:

-(void) multiStageAnimate{ [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{ //animation code } completion:^(BOOL finished){ if(/* If terminating condition not met*/) [self multiStageAnimate]; }]; } 

Comments

0

I realize this is an older question, but I thought I'd add my input.

I created a class for handling multistage animation, available here!

It only supports a single duration and option set currently, but I'll probably add more features.

Here's how you use it:

// Create New Animation MSAnimation * newAnimation = [MSAnimation newAnimationWithDuration:0.35 andOptions:UIViewAnimationOptionCurveEaseInOut]; // Add Sequence [newAnimation addNewAnimationStage:^{ greenView.center = CGPointMake(greenView.center.x, greenView.center.y + 100); }]; [newAnimation addNewAnimationStage:^{ greenView.center = CGPointMake(greenView.center.x + 100, greenView.center.y); }]; [newAnimation addNewAnimationStage:^{ greenView.center = CGPointMake(greenView.center.x, greenView.center.y + 100); }]; [newAnimation addNewAnimationStage:^{ greenView.center = CGPointMake(greenView.center.x - 50, greenView.center.y); }]; [newAnimation addNewAnimationStage:^{ greenView.frame = CGRectMake(0, 0, 100, 100); }]; // Animate Your Sequence With Completion [newAnimation animateSequenceWithCompletion:^{ NSLog(@"All finished!"); }]; 

Gives you:

Animation Demo

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.