我想知道实现一系列不同动画的最好方法是什么。例如,如果我调用下面的方法并尝试向右、向左和向上动画相同的对象,它将不起作用,因为编译器不以线性方式处理它们,而我的对象最终只是向上移动(跳过向右和向左的步骤):
-(IBAction)clickStart
{
[self Animation1];
[self Animation2];
[self Animation3];
}现在我可以这样做了,但不知何故,它对我来说有点笨拙和奇怪。假设这是Animation1的方法:
[pageShadowView setFrame:CGRectMake(100, 0, CGRectGetWidth(pageShadowView.frame), CGRectGetHeight(pageShadowView.frame))];
[UIView beginAnimations:@"Animation1" context:nil]; // Begin animation
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(Animation2)];
[pageShadowView setFrame:CGRectMake(200, 0, CGRectGetWidth(pageShadowView.frame), CGRectGetHeight(pageShadowView.frame))];
[UIView commitAnimations]; // End animations然后我可以在Animation2中做同样的事情,也就是一旦完成就调用Animation3。但老实说,这是非常混乱的,也不是一个非常清晰的编码方法。有没有办法得到一个更“线性”的代码,就像我在开始时建议的那样(这是不起作用的),或者我只能简单地接受选择器方法?
谢谢你的建议!
发布于 2011-04-01 18:06:51
如果你不需要支持iOS < 4:
[UIView animateWithDuration:0.2 animations:^{
// animation 1
} completion:^(BOOL finished){
[UIView animateWithDuration:0.2 animations:^{
// animation 2
} completion^(BOOL finished){
[UIView animateWithDuration:0.2 animations:^{
// animation 3
}];
}];
}]发布于 2011-07-28 15:02:17
我们创建了一个组件,用于使用块(CPAnimationSequence on Github)以声明方式链接动画步骤。
它提供了可读性非常好的代码,如下所示:
[[CPAnimationSequence sequenceWithSteps:
[CPAnimationStep for:0.25 animate:^{ self.imageView.alpha = 0.0; }],
[CPAnimationStep for:0.25 animate:^{ self.headline.alpha = 0.0; }],
[CPAnimationStep for:0.25 animate:^{ self.content.alpha = 0.0; }],
[CPAnimationStep after:1.0 for:0.25 animate:^{ self.headline.alpha = 1.0; }],
[CPAnimationStep for:0.25 animate:^{ self.content.alpha = 1.0; }],
nil]
runAnimated:YES];与通常的基于块的方法(由Max很好地描述)相比,它具有匹配意图及其表示的优势:要动画的线性步骤序列。我们在an article on our iOS development blog中详细介绍了这个主题。
https://stackoverflow.com/questions/5511852
复制相似问题