使用块的多阶段animation
我知道你可以使用如下块来执行两阶段animation:
[UIView animateWithDuration:25.0 delay:0.0 options:UIViewAnimationCurveLinear animations: ^{ aView.alpha = 2.5; } completion:^(BOOL finished) { aView.hidden = YES; } ];
..但我如何创build一个多阶段(超过2)使用块的animation?
使用嵌套的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.. }];}];
或者你可以做一个recursion的,多阶段的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]; }]; }
我意识到这是一个老问题,但我想我会添加我的input。
我创build了一个处理多级animation的类,可以在这里find!
它目前只支持一个持续时间和选项,但是我可能会添加更多的function。
以下是你如何使用它:
// 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!"); }];
给你: