2011-04-01 196 views
2

我在想什么最好的方法是实现一系列不同的动画。如果我调用以下方法并尝试对同一个对象进行右,左和上移动,例如,它将不起作用,因为编译器没有以线性方式处理它们,并且最终得到了我的对象简单地往上走(跳过步骤走左,右):iphone连续动画

-(IBAction)clickStart 
{ 
    [self Animation1]; 
    [self Animation2]; 
    [self Animation3]; 

} 

现在我所能,这一点,但它在某种程度上感觉繁琐,怪我。考虑这是动画1的方法:

[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,即调用动画3一旦完成。但说实话,这是非常混乱,不是一个非常明确的编码方法。无论如何,要获得像我在开始时所建议的那样的“线性”代码(这是行不通的),还是我只需要使用选择器方法?

感谢您的任何建议!

回答

10

如果你没有支持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 
     }]; 
    }]; 
}] 
+0

非常感谢。尽管一见钟情,但我认为这实际上比选择器方法做得更清楚。至少我有代码以线性方式呈现。谢谢! – 2011-04-01 10:21:07

+1

块意味着您不必在通常会导致意大利面代码的课程级别存储动画状态。 – mxcl 2011-04-01 11:56:54

2

我们创建了一个组件,用于链接声明使用的块(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中详细阐述了此主题。