Multistage animation using blocks

2019-02-10 09:40发布

问题:

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:

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..
                                                              }];}];


回答2:

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];
                 }];
}


回答3:

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: