CAShapeLayer笔触看起来已重置

时间:2019-03-25 16:06:33

标签: ios objective-c cashapelayer

我有一个简单的循环进度视图,该视图在下载文件时被填充。在大多数情况下,它会按预期工作,但有时会“重置”-它填充到特定部分,通常填充到100%之前的最后一部分,然后突然变为0%,然后达到100%。这是它的实现:

@interface CircleView()

@property (nonatomic, strong) CAShapeLayer *circleLayer;

@end


@implementation CircleView

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];

    if (self) {
        self.backgroundColor = UIColor.clearColor;

        UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width / 2., self.frame.size.height / 2.0) radius: (self.frame.size.width - 10) / 2 startAngle:0.0 endAngle:M_PI * 2 clockwise:YES];

        CAShapeLayer *downLayer = [[CAShapeLayer alloc] init];
        downLayer.path = circlePath.CGPath;
        downLayer.fillColor = UIColor.clearColor.CGColor;
        downLayer.strokeColor = UIColor.lightGrayColor.CGColor;
        downLayer.lineWidth = 15.0;

        downLayer.strokeEnd = 1.0;

        self.circleLayer = [[CAShapeLayer alloc] init];
        self.circleLayer.path = circlePath.CGPath;
        self.circleLayer.fillColor = UIColor.clearColor.CGColor;
        self.circleLayer.strokeColor = UIColor.tubeWayRed.CGColor;
        self.circleLayer.lineWidth = 15.0;

        self.circleLayer.strokeEnd = 0.0;

        [self.layer addSublayer:downLayer];
        [self.layer addSublayer: self.circleLayer];
    }

    return self;
}

-(void)animateCircleToPart:(CGFloat)toPart {
    self.circleLayer.strokeEnd = toPart;
}

-(void)resetCircle {
    NSLog(@"Resetting circle");
    [CATransaction begin];
    [CATransaction setDisableActions:YES];
    self.circleLayer.strokeEnd = 0;
    [CATransaction commit];
}

@end

日志显示在达到100%之前,尚未通过reset方法对其进行重置。有什么想法可能是错误的吗?谢谢。

问题消失了,将呼叫self.circleLayer.strokeEnd = toPart;封装在[CATransaction begin]-[CATransaction commit]个呼叫中,如下所示:

-(void)animateCircleToPart: (CGFloat)toPart {
    [CATransaction begin];
    self.circleLayer.strokeEnd = toPart;
    [CATransaction commit];
}

有人可以解释为什么吗?

1 个答案:

答案 0 :(得分:0)

我的建议是这样的:

 -(void)animateCircleToPart:(CGFloat)toPart {
if (toPart > self.circleLayer.strokeEnd){
    self.circleLayer.strokeEnd = toPart;}
 }