圆弧段的笔画animation以完整笔画结束

我正在试图创build一个圆的一部分的外观animation。 为了存档,我使用了安静的CABasicAnimations。

animation从上面开始,很好地移动到整个圈子的三分之一。 但是当animation结束时,圈子正在被完全画出。

我怎样才能防止呢?

这是我的自定义UIView的源代码:

- (void)drawRect:(CGRect)rect { int radius = 100; int strokeWidth = 10; CGColorRef color = [UIColor redColor].CGColor; int timeInSeconds = 5; CGFloat startAngle = 0; CGFloat endAngle = 0.33; CAShapeLayer *circle = [CAShapeLayer layer]; circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius) cornerRadius:radius].CGPath; circle.position = CGPointMake(CGRectGetMidX(self.frame)-radius, CGRectGetMidY(self.frame)-radius); circle.fillColor = [UIColor clearColor].CGColor; circle.strokeColor = color; circle.lineWidth = strokeWidth; [self.layer addSublayer:circle]; CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; drawAnimation.duration = timeInSeconds; drawAnimation.repeatCount = 1.0; drawAnimation.removedOnCompletion = NO; drawAnimation.fromValue = [NSNumber numberWithFloat:startAngle]; drawAnimation.toValue = [NSNumber numberWithFloat:endAngle]; drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; [circle addAnimation:drawAnimation forKey:@"drawCircleAnimation"]; } 

当您将animation应用于图层时,Core Animation会创build该图层的副本并为该副本的属性设置animation。 原始图层称为模型图层 ,副本称为表示图层 。 animation不会改变模型图层的属性。

您试图通过将removedOnCompletion设置为NO来解决此问题。 你还必须设置animation的fillMode来使这个工作,但它不是真正的animation属性的正确方法。

正确的方法是更改​​模型图层上的属性,然后应用animation。

 // Change the model layer's property first. circle.strokeEnd = endAngle; // Then apply the animation. CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; drawAnimation.duration = timeInSeconds; drawAnimation.fromValue = [NSNumber numberWithFloat:startAngle]; drawAnimation.toValue = [NSNumber numberWithFloat:endAngle]; drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; [circle addAnimation:drawAnimation forKey:@"drawCircleAnimation"]; 

这在WWDC 2011的Core Animation Essentialsvideo中进行了解释。 我强烈build议你看。