【问题标题】:CAShapeLayer stroke looks resetCAShapeLayer 笔触看起来已重置
【发布时间】:2019-03-25 16:06:32
【问题描述】:

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

有人能解释一下原因吗?

【问题讨论】:

  • 如果您只在初始化程序中设置strokeEnd、animateCircleToPart: 和resetCircle,并且您说reset 没有被称为它很可能是应用程序的其他部分使用意外的低值(例如 0)调用 animateCircleToPart:。您应该能够通过打印良好的值来确认这一点。如果是这种情况,那么我会查看您从哪里调用 animateCircleToPart:,以了解意外低值可能来自哪里。
  • @DavidRönnqvist 感谢您的意见。我记录了传递给animateCircleToPart: 的值,并且没有任何值出乎意料地低。从低至 0.033096 开始,然后上升到 1。
  • 我也从MRProgress 尝试了与MRCircularProgressView 相同的逻辑,问题就消失了。但是,更新是以相同的方式进行的。我只是在改变情节提要中的视图。带回 CircleView 会重新引入问题。

标签: ios objective-c cashapelayer


【解决方案1】:

我的建议是这样的:

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

【讨论】:

  • 当输入异常低时,与其什么都不做,不如调试问题并查看低值来自何处并阻止那部分代码调用animateCircleToPart: ?
  • 也许会更好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-06
  • 2021-04-03
  • 1970-01-01
相关资源
最近更新 更多