Search code examples
ioscocoa-touchcore-animation

IOS: problem with alpha


I have this code, where successview start with alpha = 0.00

- (void) startAnimation{
    //immediately
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:3];
    [successView setAlpha:1.00];
    [UIView commitAnimations];
    //in three seconds
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:3];
    [successView setAlpha:0.00];
    [UIView commitAnimations];
}

in this way, in first animation (alpha 0.00 to 1.00), it don't happen in 3 seconds but immediately, instead in the second animation (alpha 1.00 to alpha 0.00) it happens in 3 seconds

if I write only firts animation:

- (void) startAnimation{
    //in three seconds
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:3];
    [successView setAlpha:1.00];
    [UIView commitAnimations];
}

it happens in 3 seconds, why in the forst example it don't happen?


Solution

  • - (void)startAnimation
    {
        [UIView beginAnimations:@"successAnimationPart1" context:nil];
        [UIView setAnimationDuration:3];
        [UIView setAnimationDelegate:self];
        [successView setAlpha:1.00];
        [UIView commitAnimations];
    }
    

    And add this animation delegate method:

    - (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
    {
        if ([animationID isEqualToString:@"successAnimationPart1"]) {
            // when first animation is done, call second
            [UIView beginAnimations:@"successAnimationPart2" context:nil];
            [UIView setAnimationDuration:3];
            [UIView setAnimationDelegate:self];
            [successView setAlpha:0.00];
            [UIView commitAnimations];
        }
    }