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?
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:
-(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!
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:
精彩评论