Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

iphone - iOS: How to detect when an animation is finished?

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

}

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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*/
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...