Установка lineWidth UIBezierPath не работает - PullRequest
0 голосов
/ 31 мая 2018

Я пытаюсь нарисовать круг, но граница круга всегда одинаково тонкая, независимо от того, какое значение я установил.Это похоже на линию с самой тонкой шириной по умолчанию.

- (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 голосов
/ 31 мая 2018

Вам нужно изменить ширину 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];

}
...