UIBezierpath setLineDash粗细

时间:2019-03-08 01:27:15

标签: ios uibezierpath

- (void)drawRect:(CGRect)rect {
[[UIColor redColor] set];

UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 2.f;
[path addArcWithCenter:CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2) radius:self.frame.size.width / 2 startAngle:M_PI_2 endAngle:M_PI * 3 / 2.f clockwise:YES];
[path addLineToPoint:CGPointMake(self.frame.size.width, 0)];
[path addLineToPoint:CGPointMake(self.frame.size.width, self.frame.size.height)];
[path closePath];

CGFloat dashPattern[] = {4.f,1.f}; //make your pattern here

[path setLineDash:dashPattern count:2 phase:0.f];

[path stroke];

} enter image description here

从上面可以看到,路径线宽设置为2.f,但是圆弧的线宽比线宽。这段代码出了什么问题?如何使弧和线具有相同的宽度?任何答案都将不胜感激。 ^ _ ^

已编辑:

借助rob mayoff的帮助,该问题已解决。

- (void)drawRect:(CGRect)rect {
    CGFloat dash[] = {4, 1};
    [[UIColor orangeColor] set];
    UIBezierPath *roundPath = [UIBezierPath bezierPath];

    [roundPath addArcWithCenter:CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2) radius:(self.frame.size.width / 2 - 0.5) startAngle:M_PI_2 endAngle:M_PI * 3 / 2.f clockwise:YES];
    [roundPath setLineDash:dash count:2 phase:0];
    [roundPath stroke];

    UIBezierPath *linePath = [UIBezierPath bezierPath];
    linePath.lineWidth = 2.f;
    [linePath moveToPoint:CGPointMake(self.frame.size.width / 2, 0)];
    [linePath addLineToPoint:CGPointMake(self.frame.size.width, 0)];
    [linePath moveToPoint:CGPointMake(self.frame.size.width, self.frame.size.height)];
    [linePath addLineToPoint:CGPointMake(self.frame.size.width / 2, self.frame.size.height)];
    [linePath setLineDash:dash count:2 phase:0];
    [linePath stroke];
}

enter image description here

0 个答案:

没有答案