gpt4 book ai didi

objective-c - 圆段的笔画动画以完整的笔画结束

转载 作者:可可西里 更新时间:2023-11-01 03:26:30 27 4
gpt4 key购买 nike

我正在尝试为圆的一部分的外观设置动画。为了归档这个,我使用了一个 CABasicAnimations,它工作得很好。

动画从顶部开始,然后安静地很好地移动到整个圆的三分之一。但是当动画结束时,圆立即被完全绘制出来。

我怎样才能防止这种情况发生?

这是我的自定义 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"];
}

最佳答案

当您将动画应用于层时,Core Animation 会创建层的副本并为副本的属性设置动画。原始层称为模型层,副本称为表示层。动画永远不会改变模型层的属性。

您尝试通过将 removedOnCompletion 设置为 NO 来解决此问题。您还必须设置动画的 fillMode 才能使其正常工作,但这并不是为属性设置动画的真正正确方法。

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

// 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"];

这在 Core Animation Essentials video 中有解释来自 WWDC 2011 .我强烈推荐观看。

关于objective-c - 圆段的笔画动画以完整的笔画结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9142888/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com