一些重绘后,带有弧UIBezierPath的CALayer错误地绘制

时间:2014-07-30 12:57:36

标签: ios calayer drawrect uibezierpath

我创建了一个CALayer,其中 路径 属性设置为调用addArcWithCenter类的UIBezierPath方法的结果。该代码绘制了我需要的内容,如左图所示,但经过几次重新绘制(执行drawRect方法)后,弧绘制不正确,如下面附带的第2张和第3张图片。

有人可以帮我找到可能出现的错误吗?

代码是下一个:

- (void)drawRect:(CGRect)rect{
    if (_remainingLayer){
        [_remainingLayer removeFromSuperlayer];
    }
    else{
        self.remainingLayer = [CAShapeLayer layer];
    }

    CGFloat viewRadius = self.frame.size.width / 2.0;
    CGPoint center = CGPointMake(viewRadius, self.frame.size.height / 2.0);
    CGFloat percent = 1/24.0f;

    // create an arc path
    UIBezierPath *path = [UIBezierPath bezierPath];
    CGFloat startAngle = 3 * M_PI / 2;
    CGFloat endAngle;
    if (clockwise == NO){
        endAngle = startAngle - (2 * M_PI) * percent;
    }
    else{
        endAngle = startAngle + (2 * M_PI) * percent;
    }

    UIBezierPath *path = [path addArcWithCenter:location
                                         radius:radius
                                     startAngle:startAngle
                                       endAngle:endAngle
                                      clockwise:YES];

    self.remainingLayer.path = path;
    self.remainingLayer.strokeColor = [self.greyColor CGColor];
    self.remainingLayer.fillColor = [[UIColor clearColor] CGColor];
    self.remainingLayer.lineWidth = kMFRoundWidth;
    [self.layer addSublayer:_remainingLayer];
}

Illustration image][1]

0 个答案:

没有答案