Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];
}
like image 392
mablecable Avatar asked Jan 13 '23 21:01

mablecable


2 Answers

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
        }];
    }];
}];
like image 152
rmaddy Avatar answered Jan 19 '23 10:01

rmaddy


You should have them performSelector:withObject:afterDelay: instead.

So change your code to:

- (void)viewDidLoad
{
    [self performSelector:@selector(fadeAnimation:) withObject:lblReady afterDelay:0];
    [self performSelector:@selector(fadeAnimation:) withObject:lblSet afterDelay:2];
    [self performSelector:@selector(fadeAnimation:) withObject:lblGo afterDelay:4];
}

-(void)fadeAnimation:(UIView *)target {
    [self fadeAnimation:@"fadeAnimation finished:YES target:target];
}




- (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];
}

This just calls each action code after 0, 2, or 4 sections. If the animation duration is changed, those numbers should be changed correspondingly.

If you want to use block animations instead of using the old animation style, you could instead do:

[UIView animateWithDuration:2 animations ^{
    //put your animations in here
} completion ^(BOOL finished) {
    //put anything that happens after animations are done in here.
    //could be another animation block to chain animations
}];
like image 37
Jsdodgers Avatar answered Jan 19 '23 10:01

Jsdodgers