3

我正在尝试制作多阶段动画,以便 UIImageView (1) 淡入,(2) 移动,(3) 滑出屏幕。

似乎只有第 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

2 回答 2

4

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

[UIView setAnimationDelegate:self];

在最后一个动画块中将自己取消设置为代理(设置为 nil)是一个好主意。

于 2009-06-15T20:10:11.763 回答
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
}
于 2009-11-02T02:13:41.483 回答