简体   繁体   English

iOS绘图弧无法按预期工作

[英]iOS drawing arcs not working as expected

I'm trying to draw a simple arc with Quartz Core but I'm not getting the expected result. 我正在尝试使用Quartz Core绘制一个简单的圆弧,但没有得到预期的结果。

My arc is a basic 0 degrees to 90 degree arc (counter-clockwise direction). 我的弧是基本的0度到90度弧(逆时针方向)。

I have this code here: 我在这里有此代码:

- (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);

}

Note: MathTools is just a convenient class I create for converting degrees to radians and vice versa, the implementation for degreesToRadians: is: 注意: MathTools只是我创建的一个方便的类,用于将度转换为弧度,反之亦然, degreesToRadians:的实现是:

+(CGFloat)degreesToRadians:(CGFloat)degrees
{
    return degrees * M_PI / 180.0;
}

But instead of seeing a white arc inside my purple circle, all I see is white dash: 但是,我没有看到紫色圆圈内的白色弧线,而是看到白色破折号:

圆弧无

I'm trying to get it to look like this: 我正在尝试使其看起来像这样:

预期结果

Edit 编辑

Based on answer given by rmaddy, my new code looks like this: 根据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);
}

Not sure if this helps anyone else but based on what I see here, it seems Apple's angle positive and negative direction is not the same as mathematics. 不确定这是否对其他人有帮助,但根据我在这里看到的内容,似乎苹果公司的正反角度与数学方向不同。 From memory, mathematic +90 degrees is anti-clockwise, but Apple's +90 degrees seems like clockwise. 从内存来看,数学上的+90度是逆时针的,但是苹果公​​司的+90度似乎是顺时针的。

One critical issue I see is your use of self.center . 我看到的一个关键问题是您对self.center的使用。 Your intent is to move to the center of the view but self.center is relative to the view's frame, not its bounds. 您的意图是移动到视图的中心,但self.center相对于视图的框架,而不是其边界。

Everything in drawRect: needs to be relative to the view's bounds, not its frame. drawRect:所有内容都必须相对于视图的边界,而不是其框架。

Change this line: 更改此行:

CGContextMoveToPoint(ctx, self.center.x, self.center.y);

to: 至:

CGContextMoveToPoint(ctx, CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));

Make a similar change to the call to CGContextAddArc . CGContextAddArc的调用进行类似的更改。

BTW - the only time self.center would work for this case is if the view's origin is at 0, 0 . 顺便说一句-在这种情况下, self.center唯一适用的时间是视图的原点位于0, 0 With any other origin, self.center will give you a problem for this case. 对于任何其他来源, self.center都会给您带来这种情况的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM