我使用以下代码为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
,我也想知道为什么图层返回到原始图层。
您可以将FillMode
属性与removedOnCompletion
属性结合使用,以在repeatCount
结束后将第一帧设置回其内容。
[countanimation setFillMode:kCAFillModeBoth];
[countanimation setRemovedOnCompletion:NO];
我认为这两行代码可以帮助您获得所需的结果。
我使用animation.fillMode = kCAFillModeForwards;
解决了此问题
我将原始图像替换为number1,并解决了问题。
Swift 4 + 5
更新Jami对Swift 4和5的回答
colorAnimation.fillMode = .both
colorAnimation.isRemovedOnCompletion = false