gpt4 book ai didi

ios - CAShapeLayer 动画不会停留在屏幕上而是消失

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:06:45 25 4
gpt4 key购买 nike

我正在尝试绘制一个动画圆圈,但每个部分都需要另一种颜色。现在一切正常,只是在我再次调用该方法之前刚刚绘制的作品消失了,所以只剩下最后一部分。我不想要那个,我想要在 4 次之后用不同颜色的笔触绘制一个完整的圆圈。

如何解决这个问题使其不消失?

这是我的初始化代码:

- (void)_initCircle {
_circle = [CAShapeLayer layer];
_radius = 100;

// Make a circular shape
_circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0 * _radius, 2.0 * _radius) cornerRadius:_radius].CGPath;

// Center the shape in self.view
_circle.position = CGPointMake(CGRectGetMidX(self.view.frame) - _radius,
CGRectGetMidY(self.view.frame) - _radius);

_circle.fillColor = [UIColor clearColor].CGColor;
_circle.lineWidth = 5;

// Add to parent layer
[self.view.layer addSublayer:_circle];
}

这是我的画作:

- (void)_drawStrokeOnCircleFrom:(float)start to:(float)end withColor:(UIColor *)color {
// Configure the apperence of the circle
_circle.strokeColor = color.CGColor;
_circle.strokeStart = start;
_circle.strokeEnd = end;

[CATransaction begin];

// Configure animation
CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
drawAnimation.duration = 2.0; // "animate over 10 seconds or so.."
drawAnimation.repeatCount = 1.0; // Animate only once..

// Animate from no part of the stroke being drawn to the entire stroke being drawn
drawAnimation.fromValue = [NSNumber numberWithFloat:start];
drawAnimation.toValue = [NSNumber numberWithFloat:end];

// Experiment with timing to get the appearence to look the way you want
drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

[CATransaction setCompletionBlock:^{
NSLog(@"Complete animation");
_index++;
if(_index < _votes.count){
_startStroke = _endStroke;
_endStroke += [_votes[_index] floatValue] / _totalListens;
[self _drawStrokeOnCircleFrom:_startStroke to:_endStroke withColor:_colors[_index]];
}

}];

// Add the animation to the circle
[_circle addAnimation:drawAnimation forKey:@"drawCircleAnimation"];

[CATransaction commit];
}

最佳答案

尝试在您的 CABasicAnimation 上设置这些值:

drawAnimation.removedOnCompletion = NO;
drawAnimation.fillMode = kCAFillModeForwards;

关于ios - CAShapeLayer 动画不会停留在屏幕上而是消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29209786/

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