CAShapeLayer笔划看起来重置

问题描述 投票:0回答:1

我有一个简单的循环进度视图,在下载文件时会被填充。大部分时间它按预期工作,但有时它会“重置” - 它会被填充到某个部分,通常到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];
}

有人能解释为什么吗?

ios objective-c cashapelayer
1个回答
0
投票

我的建议是这样的:

 -(void)animateCircleToPart:(CGFloat)toPart {
if (toPart > self.circleLayer.strokeEnd){
    self.circleLayer.strokeEnd = toPart;}
 }
© www.soinside.com 2019 - 2024. All rights reserved.