繁体   English   中英

多级 animation 使用块

[英]Multistage animation using blocks

我知道您可以使用如下块执行两阶段动画:

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

..但是如何使用块创建多级(超过 2 个)animation?

使用嵌套动画:

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

或者您可以制作一个递归的多阶段 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];
                 }];
}

我意识到这是一个较老的问题,但我想我会添加我的输入。

我创建了一个 class 用于处理多级 animation,可在此处获得!

它目前仅支持单个持续时间和选项集,但我可能会添加更多功能。

以下是你如何使用它:

// 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!");
}];

给你:

动画演示

暂无
暂无

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

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