Search code examples
iosobjective-canimationchaining

How to chain fade animations


I have 3 UILabels that I want to fade out, one after the other after a couple of seconds. My problem is these are happening all at once. I am trying to chain the animations, but I cannot get this to work. I've tried all sorts of suggestions but to no avail. I know it cannot be this hard. I preferably would like to bundle these together in one animation method, because I would like to trigger other functionality from the animationDidStop after all 3 labels have been displayed. Any help or suggestions??

Here is my code:

- (void)viewDidLoad
{
    [self fadeAnimation:@"fadeAnimation" finished:YES target:lblReady];
    [self fadeAnimation:@"fadeAnimation" finished:YES target:lblSet];
    [self fadeAnimation:@"fadeAnimation" finished:YES target:lblGo];
}


- (void)fadeAnimation:(NSString *)animationID finished:(BOOL)finished target:(UIView *)target
{
    [UIView beginAnimations:nil context:nil];
    [UIView beginAnimations:animationID context:(__bridge void *)(target)];
    [UIView setAnimationDuration:2];

    [target setAlpha:0.0f];
    [UIView setAnimationDelegate:self];    
    [UIView commitAnimations];
}

Solution

  • This would be easier with the latest UIView animation methods:

    [UIView animateWithDuration:2.0 animations:^ {
        lblReady.alpha = 0;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:2.0 animations:^ {
            lblSet.alpha = 0;
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:2.0 animations:^ {
                lblGo.alpha = 0;
            } completion:^(BOOL finished) {
                // Add your final post-animation code here
            }];
        }];
    }];