iOS: How to detect when an animation is finished?

14,143

Solution 1

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

Solution 2

The animation is finished when [animation isAnimating] == NO.

If you want to wait until it is finished but not block the UI:

while ([animation isAnimating]) {
    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.05]];
}
Share:
14,143
thar
Author by

thar

Updated on June 14, 2022

Comments

  • thar
    thar almost 2 years

    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]; 
    
    }
    
    }
    
  • morningstar
    morningstar over 12 years
    I don't know if that works for animationImages type animation. Only works with animations using [UIView beginAnimations:context:].
  • Nuthinking
    Nuthinking almost 11 years
    This doesn't seem to work anymore: stackoverflow.com/questions/3144300/…
  • zaph
    zaph almost 11 years
    Probably best now to use: + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion