简体   繁体   English

使用核心图形为iOS绘制空心圆

[英]Drawing a Hollow Circle with Core Graphics for iOS

I am looking for the way how to draw a hollow circle using Core Graphics ( CGContext ) in iOS. 我正在寻找如何在iOS中使用Core Graphics( CGContext )绘制空心圆的方法。 I tried to do it using the code: 我尝试使用代码来做到这一点:

- (void)drawRect:(CGRect)rect {
      [super drawRect:rect];
      CGContextRef ctx = UIGraphicsGetCurrentContext();
      CGContextSetLineWidth(ctx, 8.0);
      CGContextSetStrokeColor(ctx, CGColorGetComponents([[UIColor redColor] CGColor]));
      CGContextSetFillColor(ctx, CGColorGetComponents([[UIColor blueColor] CGColor]));
      CGContextFillEllipseInRect(ctx, rect);
      CGContextFillPath(ctx);
}

but it draws the filled circle. 但它画出了圆圈。 Since this question is over discussed here but other examples like this give me only filled circles. 由于这个问题在这里已经讨论过了,但是像这样的其他例子只给我填充了圆圈。

If you want to draw a circle within the rect, using Stroke methods and the rect in parameter you will draw a part of the cercle outside the rect. 如果你想在rect中绘制一个圆,使用Stroke方法和rect in参数,你将在rect之外绘制一部分cercle。

You then have two choices, assuming you know the width of the circle you want to draw. 假设您知道要绘制的圆的宽度,那么您有两个选择。

First, you can stroke the line, but you have to draw it in a smaller rect : 首先,你可以划线,但你必须在较小的矩形中绘制它:

-(void)drawRect:(CGRect)rect {

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGFloat innerWidth = 8;
    [[UIColor redColor] setStroke];

    CGContextSetLineWidth(ctx, innerWidth);

    // We add an ellipsis shifted by half the inner Width
    CGContextAddEllipseInRect(ctx, CGRectInset(rect, innerWidth, innerWidth));

    // Stroke the path
    CGContextStrokePath(ctx);
}

You can also fill the circles using the even-odd rule ( Apple docs ) 您也可以使用偶数规则填充圆圈( Apple文档

-(void)drawRect:(CGRect)rect {

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGFloat innerWidth = 8;
    [[UIColor redColor] setStroke];

    // Add the outer circle
    CGContextAddEllipseInRect(ctx, rect);
    // Add the inner circle
    CGContextAddEllipseInRect(ctx, CGRectInset(rect, innerWidth, innerWidth));

    // Fill the path using the EO rule
    CGContextEOFillPath(ctx);
}

If you only want the circle outline, don't fill it. 如果您只想要圆形轮廓,请不要填充它。

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(ctx, 8.0);
    [[UIColor redColor] set];
    CGContextStrokeEllipseInRect(ctx, rect);
}

Use CGContextStrokeEllipseInRect instead of CGContextFillEllipseInRect . 使用CGContextStrokeEllipseInRect而不是CGContextFillEllipseInRect And since you haven't built a path in the current context, get rid of CGContextFillPath . 由于您尚未在当前上下文中构建路径,因此请删除CGContextFillPath

One way you could do it is draw a filled circle and then draw a slightly smaller circle with CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear) 你可以做的一种方法是绘制一个实心圆,然后使用CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear)绘制一个稍微小一点的圆圈

After your code add something like 在你的代码添加之后

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ctx, 5.0);
CGContextSetBlendMode(ctx,kCGBlendModeClear)
CGContextSetFillColor(ctx, CGColorGetComponents([[UIColor blueColor] CGColor]));
CGContextFillEllipseInRect(ctx, rect);
CGContextFillPath(ctx);

UIGraphicsEndImageContext();

I use CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear) in my drawing application, which is probably a better use. 我在我的绘图应用程序中使用CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear) ,这可能是一个更好的用法。 Stroking the path will probably work for you. 抚摸这条路可能对你有用。

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

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