设置UIBezierPath的lineWidth不起作用

时间:2018-05-31 07:34:37

标签: ios objective-c core-graphics core-animation uibezierpath

我正在尝试绘制一个圆圈,但无论我设置什么值,圆圈边框总是相同的。 感觉就像线条始终是默认的最薄宽度。

- (void)drawCircleAtPoint:(CGPoint)center radius: (CGFloat)radius{

    UIBezierPath *path = [UIBezierPath bezierPath];

    path.lineWidth = 4; // Here
    path.lineCapStyle = kCGLineCapRound;
    path.lineJoinStyle = kCGLineJoinRound;

    CGFloat r = radius;


    [path addArcWithCenter:center radius:r startAngle:0.0 endAngle:M_PI*2 clockwise:true];

    [path stroke];

    CAShapeLayer* layer = [CAShapeLayer new];
    layer.path = path.CGPath;
    layer.strokeColor = UIColor.redColor.CGColor;
    layer.fillColor = UIColor.yellowColor.CGColor;

    [layer addAnimation:[self getAnimation] forKey:nil];
    [self.layer addSublayer:layer];

}


- (CABasicAnimation*)getAnimation {

    CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.duration = 1.2;
    animation.fromValue = @0;
    animation.toValue = @1;
    return animation;

}

这真让我疯狂。

1 个答案:

答案 0 :(得分:3)

您需要更改CAShapeLayer的宽度而不是UIBezierPath。像这样:

- (void)drawCircleAtPoint:(CGPoint)center radius: (CGFloat)radius{
    CGFloat r = radius;

    UIBezierPath *path = [UIBezierPath bezierPath];
    [path addArcWithCenter:center radius:r startAngle:0.0 endAngle:M_PI*2 clockwise:true];
    [path setLineWidth:4]; // No need
    [path setLineCapStyle:kCGLineCapRound];
    [path setLineJoinStyle:kCGLineJoinRound];
    [path stroke];

    CAShapeLayer* layer = [CAShapeLayer new];
    layer.lineWidth = 4; // Add it here 
    layer.path = path.CGPath;
    layer.strokeColor = UIColor.redColor.CGColor;
    layer.fillColor = UIColor.yellowColor.CGColor;

    [layer addAnimation:[self getAnimation] forKey:nil];
    [self.view.layer addSublayer:layer];

}