简体   繁体   English

多级 animation 使用块

[英]Multistage animation using blocks

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? ..但是如何使用块创建多级(超过 2 个)animation?

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

or you can make a recursive, multi-stage animation method:或者您可以制作一个递归的多阶段 animation 方法:

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

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!我创建了一个 class 用于处理多级 animation,可在此处获得!

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:给你:

动画演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM