iOS 绘制圆弧未按预期工作
iOS drawing arcs not working as expected
我正在尝试使用 Quartz Core 绘制一个简单的圆弧,但没有得到预期的结果。
我的弧度是0度到90度的基本弧度(counter-clockwise方向)。
我这里有这段代码:
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(ctx, self.strokeColor.CGColor);
CGContextMoveToPoint(ctx, self.center.x, self.center.y);
CGFloat radius = self.bounds.size.width / 2.0;
CGContextAddArc(ctx, self.center.x, self.center.y, radius, [MathTools degreesToRadians:0], [MathTools degreesToRadians:90], 0);
CGContextStrokePath(ctx);
}
注意:MathTools
只是一个方便的 class 我创建用于将度数转换为弧度,反之亦然,degreesToRadians:
的实现是:
+(CGFloat)degreesToRadians:(CGFloat)degrees
{
return degrees * M_PI / 180.0;
}
但是我看到的不是紫色圆圈内的白色弧线,而是白色破折号:
我试图让它看起来像这样:
编辑
根据 rmaddy 给出的答案,我的新代码如下所示:
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(ctx, self.strokeColor.CGColor);
CGContextSetLineWidth(ctx, self.strokeWidth);
CGFloat radius = self.bounds.size.width / 2.0 - self.strokeWidth;
CGContextAddArc(ctx, CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds), radius, [MathTools degreesToRadians:0], [MathTools degreesToRadians:-90], 1);
CGContextStrokePath(ctx);
}
不确定这是否对其他人有帮助,但根据我在这里看到的,Apple 的角度正负方向似乎与数学不同。记忆中数学上的+90度是anti-clockwise,但是苹果的+90度好像是顺时针
我看到的一个关键问题是您对 self.center
的使用。您的意图是移动到视图的中心,但 self.center
是相对于视图的框架,而不是它的边界。
drawRect:
中的所有内容都需要相对于视图的边界,而不是它的框架。
更改此行:
CGContextMoveToPoint(ctx, self.center.x, self.center.y);
至:
CGContextMoveToPoint(ctx, CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
对 CGContextAddArc
的调用进行类似的更改。
顺便说一句 - self.center
唯一适用于这种情况的情况是视图的原点位于 0, 0
。对于任何其他来源,self.center
都会给您带来问题。
我正在尝试使用 Quartz Core 绘制一个简单的圆弧,但没有得到预期的结果。
我的弧度是0度到90度的基本弧度(counter-clockwise方向)。
我这里有这段代码:
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(ctx, self.strokeColor.CGColor);
CGContextMoveToPoint(ctx, self.center.x, self.center.y);
CGFloat radius = self.bounds.size.width / 2.0;
CGContextAddArc(ctx, self.center.x, self.center.y, radius, [MathTools degreesToRadians:0], [MathTools degreesToRadians:90], 0);
CGContextStrokePath(ctx);
}
注意:MathTools
只是一个方便的 class 我创建用于将度数转换为弧度,反之亦然,degreesToRadians:
的实现是:
+(CGFloat)degreesToRadians:(CGFloat)degrees
{
return degrees * M_PI / 180.0;
}
但是我看到的不是紫色圆圈内的白色弧线,而是白色破折号:
我试图让它看起来像这样:
编辑
根据 rmaddy 给出的答案,我的新代码如下所示:
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(ctx, self.strokeColor.CGColor);
CGContextSetLineWidth(ctx, self.strokeWidth);
CGFloat radius = self.bounds.size.width / 2.0 - self.strokeWidth;
CGContextAddArc(ctx, CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds), radius, [MathTools degreesToRadians:0], [MathTools degreesToRadians:-90], 1);
CGContextStrokePath(ctx);
}
不确定这是否对其他人有帮助,但根据我在这里看到的,Apple 的角度正负方向似乎与数学不同。记忆中数学上的+90度是anti-clockwise,但是苹果的+90度好像是顺时针
我看到的一个关键问题是您对 self.center
的使用。您的意图是移动到视图的中心,但 self.center
是相对于视图的框架,而不是它的边界。
drawRect:
中的所有内容都需要相对于视图的边界,而不是它的框架。
更改此行:
CGContextMoveToPoint(ctx, self.center.x, self.center.y);
至:
CGContextMoveToPoint(ctx, CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
对 CGContextAddArc
的调用进行类似的更改。
顺便说一句 - self.center
唯一适用于这种情况的情况是视图的原点位于 0, 0
。对于任何其他来源,self.center
都会给您带来问题。