2009-06-15 42 views
3

我试图做一个多级动画,以便UIImageView(1)淡入,(2)移动,(3)滑出屏幕。如何创建多阶段UIImageView动画?

只有阶段1似乎工作。我究竟做错了什么?下面的代码:

// FIRST PART - FADE IN 
-(void)firstAnim 
{ 
    // 'sprite' is a UIImageView 
    [sprite setAlpha:0.1f]; 
    [UIView beginAnimations:@"anim1" context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:0.25]; 
    [UIView setAnimationDidStopSelector:@selector(secondAnim)]; 
    [UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
    [sprite setAlpha:1.0f]; 
    [UIView commitAnimations]; 
} 


// SECOND PART - MOVE 
-(void)secondAnim 
{ 
    [UIView beginAnimations:@"anim2" context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:0.5]; 
    [UIView setAnimationDidStopSelector:@selector(thirdAnim)]; 
    [UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
    sprite.frame = CGRectMake(170, 184, 20, 20); 
    [UIView commitAnimations]; 
} 

// THIRD PART - SLIDE OFF SCREEN 
-(void)thirdAnim 
{ 
    [UIView beginAnimations:@"anim3" context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:0.5]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
    sprite.frame = CGRectMake(170, 420, 20, 20); 
    [UIView commitAnimations]; 
} 

回答

4

您需要添加一个调用自己设置为动画委托:

[UIView setAnimationDelegate:self]; 

这将取消设置自己为委托(设置为无)是个好主意最后一个动画块。

+0

感谢。我也不得不将setAnimationDidStopSelector行设置为:[UIView setAnimationDidStopSelector:@selector(secondAnim:finished:context :)]; – cannyboy 2009-06-15 21:24:19

4

你的问题的完整的解决方案是:

1)设置动画代表

2)使用正确的选择和方法签名

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:.5]; 
[UIView setAnimationDelegate:self]; //set delegate! 
[UIView setAnimationDidStopSelector: 
    @selector(secondAnim:finished:context:)]; 


-(void)secondAnim:(NSString *)animationID 
     finished:(NSNumber *)finished 
      context:(void *)context { 

    //animation #2 
}