考虑以下动画:
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = 1.0;
pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
pathAnimation.removedOnCompletion = NO;
pathAnimation.delegate = self;
这将基本上为从一端到下一端的图层绘制动画。问题是,一旦动画完成,strokeEnd
属性将重置为0(最初设置的位置)。如何使最终值“坚持”?
我试图在animationDidStop委托方法中更改它。这大部分都有效,但即使放入CATransaction以禁用动画,也可能会导致strokeEnd
在0处短暂闪烁。我也玩additive
和cumulative
属性无济于事。有什么建议吗?
答案 0 :(得分:9)
您只需将strokeEnd
属性设置为其最终值即可!像这样:
// your current code (stripped of unnecessary stuff)
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = 1.0;
pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
// just add this to it:
theLayer.strokeEnd = 1;
// and now add the animation to theLayer
如果strokeEnd
是隐式可动画的(因此该行本身会导致动画),我们将首先关闭隐式动画:
[CATransaction setDisableActions:YES];
theLayer.strokeEnd = 1;
请注意,您的pathAnimation.removedOnCompletion = NO;
错误,其他答案kCAFillModeForwards
也是如此。两者都是基于对Core Animation如何运作的误解。