我正在尝试绘制一个圆圈,用于指示进度。进度更新可能会很快出现,我想对圆圈的更改进行动画处理。
我尝试使用以下方法执行此操作,但似乎没有任何效果。这可能吗?
- (id)initWithFrame:(CGRect)frame strokeWidth:(CGFloat)strokeWidth insets:(UIEdgeInsets)insets
{
if (self = [super initWithFrame:frame])
{
self.strokeWidth = strokeWidth;
CGPoint arcCenter = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
CGFloat radius = CGRectGetMidX(self.bounds) - insets.top - insets.bottom;
self.circlePath = [UIBezierPath bezierPathWithArcCenter:arcCenter
radius:radius
startAngle:M_PI
endAngle:-M_PI
clockwise:YES];
[self addLayer];
}
return self;
}
- (void)setProgress:(double)progress {
_progress = progress;
[self updateAnimations];
}
- (void)addLayer {
CAShapeLayer *progressLayer = [CAShapeLayer layer];
progressLayer.path = self.circlePath.CGPath;
progressLayer.strokeColor = [[UIColor whiteColor] CGColor];
progressLayer.fillColor = [[UIColor clearColor] CGColor];
progressLayer.lineWidth = self.strokeWidth;
progressLayer.strokeStart = 10.0f;
progressLayer.strokeEnd = 40.0f;
[self.layer addSublayer:progressLayer];
self.currentProgressLayer = progressLayer;
}
- (void)updateAnimations{
self.currentProgressLayer.strokeEnd = self.progress;
[self.currentProgressLayer didChangeValueForKey:@"endValue"];
}
目前,这段代码甚至不画圆,但删除progressLayer的strokeStart
和strokeEnd
将画出完整的圆圈。
如何让它从某个点开始,并根据我设置progress
属性开始绘制圆?
我想通了。strokeStart
值和strokeEnd
值从 0.0 到 1.0,所以我给它的值太高了。
因此,我刚刚将设置更新为:
- (void)setProgress:(double)progress {
_progress = progress * 0.00460;
[self updateAnimations];
}
我的答案在这里举例。通常,您应该将动画添加到CAShapeLayer。