CAKeyframeAnimation:停止后内容会改回第一张图片

5
我使用以下代码来实现对 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];
        ...
    }
}

问题在于该图层在原始图像(number3)闪烁一次后就被隐藏了。即使我将“removedOnCompletion”设置为“NO”,我仍然想知道为什么该图层会返回到原始状态。
4个回答

5
您可以使用FillMode属性与removedOnCompletion属性结合使用,在repeatCount完成后将第一帧设置回其内容。
[countanimation setFillMode:kCAFillModeBoth];
[countanimation setRemovedOnCompletion:NO];

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

我刚刚发现,如果您正在使用CAAnimationGroup,则这些设置需要在组上进行,而不是在单个动画上进行。希望这能帮助到任何来到这里的人。 - Hudson

1
我使用了animation.fillMode = kCAFillModeForwards;来解决这个问题。

0

我将原始图像替换为number1,问题得到解决。


0

Swift 4+5

更新 Jami 的答案,适用于 Swift 4 和 5

colorAnimation.fillMode = .both
colorAnimation.isRemovedOnCompletion = false

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接