0

考虑以下动画:

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 委托方法中改变它。这主要是有效的,但strokeEnd即使放在 CATransaction 中以禁用动画时,也会导致短暂的 0 闪烁。我也玩过additiveandcumulative属性无济于事。有什么建议么?

4

1 回答 1

10

您只需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 工作原理的误解。

于 2013-05-01T21:49:30.463 回答