CAKeyframeAnimation:内容在停止时变回第一张图像

问题描述 投票:4回答:4

我使用以下代码为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,我也想知道为什么图层返回到原始图层。

ios quartz-graphics caanimation cakeyframeanimation
4个回答
5
投票

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

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

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


1
投票

我使用animation.fillMode = kCAFillModeForwards;解决了此问题


0
投票

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


0
投票

Swift 4 + 5

更新Jami对Swift 4和5的回答

colorAnimation.fillMode = .both
colorAnimation.isRemovedOnCompletion = false
© www.soinside.com 2019 - 2024. All rights reserved.