iOS:如何检测动画何时完成?

时间:2011-10-14 20:06:41

标签: iphone ios objective-c uiimageview

是否有方法可以检测动画何时结束?我希望在动画结束时调用[nav setTitle:navItem]

以下是我的代码片段。希望问题很清楚,所以我可以得到一个解决方案,最好是一个例子。

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{

if(event.subtype == UIEventSubtypeMotionShake){


    NSString *imageUrl = @"";
    NSString *navItem = @"";

    int randomNumber = arc4random() % 3;

    switch (randomNumber) {
        case 0:
            imageUrl = @"pic1.png";
            navItem = @"txt1";
            break;
        case 1:
            imageUrl = @"pic2.png";
            navItem = @"txt2";
            break;
        case 2:
            imageUrl = @"pic3.png";
            navItem = @"txt3";
            break;
        default:
            break;
    }

    animation.animationImages = [NSArray arrayWithObjects:

                                 [UIImage imageNamed:@"pic1.png"],
                                 [UIImage imageNamed:@"pic3.png"],
                                 [UIImage imageNamed:@"pic2.png"],
                                 [UIImage imageNamed:@"pic1.png"],
                                 [UIImage imageNamed:@"pic2.png"],
                                 [UIImage imageNamed:@"pic3.png"], 
                                 [UIImage imageNamed:imageUrl],
                                 nil];

    UIImage *img = [UIImage imageNamed:imageUrl];
    [imageview setImage:img];

    [animation setAnimationRepeatCount:1];
    animation.animationDuration = 1;
    [animation startAnimating]; 

    [nav setTitle:navItem]; 

}

}

3 个答案:

答案 0 :(得分:10)

您可以使用setAnimationDidStopSelector委托 http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CAAnimation_class/Introduction/Introduction.html

[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];


- (void)animationDidStop:(NSString*)animationID finished:(BOOL)finished context:(void *)context {
    /*Do stuff*/
}

答案 1 :(得分:4)

动画在[animation isAnimating] == NO。

时结束

如果你想等到它完成但不阻止用户界面:

while ([animation isAnimating]) {
    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.05]];
}

答案 2 :(得分:-2)

    [self performSelector:@selector(animationFinished) withObject:nil afterDelay:1.0];

-(void)animationFinished {
    // do stuff
}

这只是假设动画将在您告诉它的时间内完成。