UIBezierPath 的开始和结束角度?

start and end angle of UIBezierPath?

我使用 UIBezierPathCAShapeLayer.

在 iOS 中为半圆编码如下
 clockWiseLayer = [[CAShapeLayer alloc] init];

CGFloat startAngle = -M_PI_2;
CGFloat endAngle = M_PI + M_PI_2;

CGFloat width = CGRectGetWidth(imageView.frame)/2.0f + 30;
CGFloat height = CGRectGetHeight(imageView.frame)/2.0f +50;
CGPoint centerPoint = CGPointMake(width, height);

float radius = CGRectGetWidth(imageView.frame)/2+10;

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 = 0.5f;

clockWiseLayer.lineWidth = 2.0f;
clockWiseLayer.borderWidth = 5.0f;

clockWiseLayer.shouldRasterize = NO;
[self.layer addSublayer:clockWiseLayer];

结果如下。

我要地球仪对面的这个蓝色半圆。

它是半圆,但我想要它的另一边,也是CounterClockWise。

我无法为此设置开始和结束角度,而 顺时针方向:否

谢谢。

你从圆的顶部开始 -M_PI_2 并在 M_PI + M_PI_2 结束(如果你想要完整的圆并使用 strokeEndstrokeStart 限制它) .然后将圆形路径设置为从路径末端的一半(图像的左侧)而不是从开始到一半(图像的右侧)绘制

CGFloat startAngle = -M_PI_2;
CGFloat endAngle = M_PI + M_PI_2;

clockWiseLayer.strokeStart = .5f;
clockWiseLayer.strokeEnd = 1.0f;

检查坐标文档: http://i.stack.imgur.com/1yJo6.png

Y轴与数学中的标准坐标系相反。

您应该从 1/2*PI(底部锚点)绘制到 3/2*PI(顶部锚点),然后将 strokeStart 设置为 0.0f,将 strokeEnd 设置为 1.0f(这样它会填满整个路径)。

使用 iOS 个常量的工作代码:

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];

我玩过 bezierPathWithArcCenter,如果 clockwise = NO 效果很奇怪。但是如果你想创建一个逆时针方向的圆形贝塞尔曲线路径,你可以创建一个顺时针方向的路径,然后用 bezierPathByReversingPath

恢复它

A new path object with the same path shape but for which the path has been created in the reverse direction.