Sam B
Sam B

Reputation: 27598

Checking When animation stopped

I am working on a simple UIImageView animation. Basically I got a bunch of image numbers from 0-9 and I am animating them using this code.

myAnimation.animationImages = myArray;
myAnimation.animationDuration = 0.7;
myAnimation.animationRepeatCount = 12;
[myAnimation startAnimating];

Here myArray is an array of uiimages i.e. 0.png, 1.png etc.

All is well and its animating just fine. What I need to know is when do I know when the animation has stopped? I could use NSTimer but for that I need a stopwatch to check when the animation starts and stops. Is there a better approach, meaning is there a way I can find out when the UIImageView stopped animating?

I looked at this thread as reference.

UIImageView startAnimating: How do you get it to stop on the last image in the series?

Upvotes: 2

Views: 1216

Answers (3)

neohope
neohope

Reputation: 1832

You can also try this:

[self performSelector:@selector(animationDone) withObject:nil afterDelay:2.0];

Upvotes: 0

Kaan Dedeoglu
Kaan Dedeoglu

Reputation: 14841

Yes, there is a much better approach than using NSTimers. If you're using iOS 4 or higher, it is better you start using block animations. try this

 [UIView animateWithDuration:(your duration) delay:(your delay) options:UIViewAnimationCurveEaseInOut animations:^{

        // here you write the animations you want


    } completion:^(BOOL finished) {
        //anything that should happen after the animations are over


    }];

Edit: oops I think I read your question wrong. In the case of UIImageView animations, I can't think of a better way than using NSTimers or scheduled events

Upvotes: 1

Joel
Joel

Reputation: 16124

Use the animationDidStopSelector. This will fire when the animation is done:

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

then implement the selector:

- (void)someMethod:(NSString*)animationID finished:(NSNumber*)finished context:(void*)context {

}

Upvotes: 4

Related Questions