iOS:如何检测动画何时完成? [英] iOS: How to detect when an animation is finished?

查看:203
本文介绍了iOS:如何检测动画何时完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a method to detect when an animation is finished? I want to call [nav setTitle:navItem] when the animation is finished.

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

Here below is a snippet of my code. Hope the question is clear enough, so I can get a solution, and preferably an example.

-(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]; 

}

}


推荐答案

您可以使用setAnimationDidStopSelector委托。

http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CAAnimation_class/Introduction/Introduction.html

You can use the setAnimationDidStopSelector delegate.
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*/
}

这篇关于iOS:如何检测动画何时完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆