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

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

并希望在动画停止时隐藏图层:
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    if([[anim valueForKey:@"name"] isEqual:@"Countdown"])
    {
        [countDown setHidden:YES];
        ...
    }
}

问题是该图层与原始图像(编号 3)一起闪烁一次,然后隐藏。

我想知道为什么即使我将 removedOnCompletion 设置为 NO 层也会回到原来的状态。

最佳答案

您可以将 FillMode 属性与 removedOnCompletion 属性结合使用,在 repeatCount 结束后将第一帧设置回其内容。

[countanimation setFillMode:kCAFillModeBoth];
[countanimation setRemovedOnCompletion:NO];

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

关于ios - CAKeyframeAnimation : contents change back to first image when stopped,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12072410/

10-12 22:01