start and end angle of UIBezierPath?

前端 未结 3 1489
遇见更好的自我
遇见更好的自我 2021-02-07 23:36

I have coded as below for half circle in iOS using UIBezierPath and CAShapeLayer.

 clockWiseLayer = [[CAShapeLayer alloc] init]         


        
3条回答
  •  醉话见心
    2021-02-08 00:00

    Check documentation for coordinates: http://i.stack.imgur.com/1yJo6.png

    Y-Axis is reversed compared to standard coordinate system in mathematics.

    You should draw from 1/2*PI (bottom anchor) to 3/2*PI (top anchor) and then you set strokeStart to 0.0f and strokeEnd to 1.0f (so it fills whole path).

    Working code using iOS constants:

    CGFloat startAngle = M_PI_2;
    CGFloat endAngle = startAngle + M_PI;
    CGFloat width = CGRectGetWidth(imageView.frame)/2.0f;
    CGFloat height = CGRectGetHeight(imageView.frame)/2.0f;
    CGPoint centerPoint = CGPointMake(width, height);
    float radius = CGRectGetWidth(imageView.frame)/2+10;
    CAShapeLayer* clockWiseLayer = [[CAShapeLayer alloc] init];
    clockWiseLayer.path = [UIBezierPath bezierPathWithArcCenter:centerPoint
                                                         radius:radius
                                                     startAngle:startAngle
                                                       endAngle:endAngle
                                                      clockwise:YES].CGPath;
    
    clockWiseLayer.fillColor = [UIColor clearColor].CGColor;
    clockWiseLayer.strokeColor = [UIColor blueColor].CGColor;
    clockWiseLayer.borderColor = [UIColor greenColor].CGColor;
    clockWiseLayer.backgroundColor = [UIColor redColor].CGColor;
    
    clockWiseLayer.strokeStart = 0.0f;
    clockWiseLayer.strokeEnd = 1.0f;
    
    clockWiseLayer.lineWidth = 2.0f;
    clockWiseLayer.borderWidth = 5.0f;
    [self.layer addSublayer:clockWiseLayer];
    

提交回复
热议问题