Search code examples
iosquartz-graphicscaanimationcakeyframeanimation

CAKeyframeAnimation : contents change back to first image when stopped


I animate the content of a CALayer using the following code:

CAKeyframeAnimation *countanimation = [CAKeyframeAnimation animation];
NSArray *images = [NSArray arrayWithObjects:(id)[UIImage imageNamed:@"number3"].CGImage, 
                        (id)[UIImage imageNamed:@"number2"].CGImage, 
                        (id)[UIImage imageNamed:@"number1"].CGImage, nil];
[countanimation setKeyPath:@"contents"];
[countanimation setValues:images];
[countanimation setCalculationMode:kCAAnimationDiscrete];
[countanimation setDuration:3.0f];
[countanimation setDelegate:self];
[countanimation setAutoreverses:NO];
[countanimation setRemovedOnCompletion:NO];
[countanimation setValue:@"Countdown" forKey:@"name"];
[countDown addAnimation:countanimation forKey:nil];

And want to hide the layer when the animation is stopped:

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    if([[anim valueForKey:@"name"] isEqual:@"Countdown"])
    {
        [countDown setHidden:YES];
        ...
    }
}

The problem is that the layer is flashing once with the original image (number3) and is hidden after that.

I want to know why the layer goes back to the original even though I set removedOnCompletion to NO.


Solution

  • I replaced the original image to number1, and the problem was solved.