Chris Newman
Chris Newman

Reputation: 2240

iPhone SDK - how can I tell when an animation has finished?

I am starting an animated enlargement when an image is touched, and then scaling it back down to normal size when it is released. By using setAnimationBeginsFromCurrentState:YES the zooming effect is nice and smooth if you lift your finger part way through animating.

However, what I want to do is "lock" the larger size in place if you've touched the image for long enough for the animation to complete, but let it shrink back down as normal if you release prematurely.

Is there a way to tell whether there is currently an animation running, or whether a particular animation has completed?

I figure I can probably do this with a performSelector:afterDelay: call in touchesStarted, with a delay equal to the length of the animation and cancelling it if touchesEnded comes too soon, but I can't imagine that's the best way...?

Upvotes: 5

Views: 1965

Answers (3)

Alex Wayne
Alex Wayne

Reputation: 186994

- (void)animateStuff {
    [UIView beginAnimations:@"animationName" context:nil];
    [UIView setAnimationDelegate:self];
    [self.view doWhatever];
    [UIView commitAnimations];
}

- (void)animationDidStop:(NSString *)animationID
                finished:(NSNumber *)finished
                 context:(void *)context
{
    if ([finished boolValue]) {
        NSLog(@"Animation Done!");
    }
}

Upvotes: 13

Abramodj
Abramodj

Reputation: 5879

Another possibility:

 [UIView animateWithDuration:0.3 animations:^{

      myView.transform = CGAffineTransformMakeRotation(M_PI);

 }completion:^(BOOL finished) {

      NSLog(@"Animation complete!");
 }];

Upvotes: 1

Chris W.
Chris W.

Reputation: 655

I think "+ (void)setAnimationDidStopSelector:(SEL)selector" should do what you want. It will call the given selector on your delegate once the animation has completed.

Upvotes: 0

Related Questions