简体   繁体   中英

IOS Core animation, how do I call animationDidStop: method when my animation sequence ends?

I have the following sequence of images to be animated:

IBOutlet UIImageView *block;
NSArray *image_arr = [NSArray arrayWithObjects:[UIImage imageNamed:@"img1"],[UIImage imagedNamed:@"img2"],nil];
block.animationImages = image_arr;
block.animationDuration =1;
block.animationRepeatcount =5;
[block startAnimating];

And the method animationDidStop:

-(void)animationDidStop:(NSString*)animationID finished:(BOOL)didFinish context:(void*)context{
NSLog(animationID);
}

What do I need to do to have the method animationDidStop called at the end of the animation?

UIImageView's "animationImages" property is not related to coreanimation, it just triggers an internal NSTimer that switched the image.

The

-(void)animationDidStop:finished:context:

selector is called as a part of core animation.

What I'd try in your case is something like this:

    IBOutlet UIImageView *block;
NSArray *image_arr = [NSArray arrayWithObjects:[UIImage imageNamed:@"img1"],[UIImage imagedNamed:@"img2"],nil];
block.animationImages = image_arr;
block.animationDuration = animationDuration;
block.animationRepeatcount = animationRepeatCount;
[block startAnimating];
[self performSelector:@selector(animationFinished:) withObject:@"someAnimationId" afterDelay: animationDuration * animationRepeatCount];

Keep in mind that this method can cause some lag - if the animation frames' images are displayed for the first time as a part of the animation, lazy loading kicks in before the first time the animation starts playing, and it may effect the precision of the performSelector call.

If this turns out to be a problem check out this answer's forceLoad method , and if that doesn't work call startAnimation on a hidden UIImageView, it did the trick for me once.

What do I need to do to have the method animationDidStop called at the end of the animation?

You can't have it called, because this is not Core Animation. You have mentally conflated two different things:

  • The ability to do very simple-minded cartoon-style animation by having a UIImageView show a succession of images;

  • Core Animation, in which there can be a completion block or completion handler.

What I suggest you do is either use real Core Animation where you can actually set up an animation and a completion block or handler, or else just fake it by using delayed performance to call back into your own code 5 seconds from now when this pseudo-animation is over.

With the latest iOS Versions 4.2 and later you should be using Blocks for animations

The following will push you in the right direction, try changing the UIViewAnimationOption to something different:

[UIView animateWithDuration:1.0f delay:0.0f options:UIViewAnimationOptionRepeat animations:^{
                //Animation here
            }completion:^(BOOL finished) {
                if(finished){
                    //Your Code here
                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM