CAKeyframeAnimation:停止时内容会变回第一张图片

Ego*_*r T 4 quartz-graphics cakeyframeanimation caanimation ios

CALayer使用以下代码为a的内容设置动画:

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];
Run Code Online (Sandbox Code Playgroud)

并希望在动画停止时隐藏图层:

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

问题在于该图层与原始图像(number3)一起闪烁一次,之后被隐藏。

我想知道为什么即使设置removedOnCompletion为,图层也能恢复到原始状态NO

小智 5

您可以FillMode结合使用property和removedOnCompletionproperty将结束后的第一帧设置回其内容repeatCount

[countanimation setFillMode:kCAFillModeBoth];
[countanimation setRemovedOnCompletion:NO];
Run Code Online (Sandbox Code Playgroud)

我认为这两行代码可以帮助您获得所需的结果。


Ego*_*r T 0

我把原来的图片替换成number1,问题就解决了。