简体   繁体   English

圆段笔划的动画以完整笔划结束

[英]Animation of stroke of circle segment ends in complete stroke

I'm trying to animate the appearance of a segment of a circle. 我试图动画一段圆的外观。 To archive this I use a CABasicAnimations which works quiet fine. 为了存档这个我使用CABasicAnimations工作安静。

The animation starts on top and moves quiet nicely to one third of the whole circle. 动画从顶部开始,很好地移动到整个圆圈的三分之一。 But when the animation finishes, the circle is being drawn completely immediately. 但是当动画结束时,圆圈会立即被完全绘制。

How can I prevent that? 我怎么能防止这种情况?

Here is the source code of my custom UIView: 这是我的自定义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"];
}

When you apply an animation to a layer, Core Animation creates a copy of the layer and animates the properties of the copy. 将动画应用于图层时,Core Animation会创建图层的副本并为副本的属性设置动画。 The original layer is called the model layer and the copy is called the presentation layer . 原始图层称为模型图层 ,副本称为表示图层 An animation never changes the properties of the model layer. 动画永远不会更改模型图层的属性。

You tried to fix this by setting removedOnCompletion to NO . 您尝试通过将removedOnCompletion设置为NO来解决此问题。 You would also have to set the fillMode of the animation to make this work, but it's not really the correct way to animate a property. 您还必须设置动画的fillMode才能使其工作,但这并不是动画属性的正确方法。

The correct way is to change the property on your model layer, then apply the 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"];

This is explained in the Core Animation Essentials video from WWDC 2011 . 这在WWDC 2011Core Animation Essentials视频中进行了解释。 I highly recommend watching it. 我强烈推荐观看它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM