如何在iPhone上绘制同心圆?

8

我想画一个环形。环应该填充在外圆中。我参考了一份文档http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_paths/dq_paths.html#//apple_ref/doc/uid/TP30001066-CH211-TPXREF101。但仍然无法获得预期的结果。以下是代码。

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextClearRect(ctx, rect);
CGContextSetRGBFillColor(ctx, 0.0, 255.0, 1.0, 1.0);CGContextFillPath(ctx);
CGContextStrokeEllipseInRect(ctx, CGRectMake(125, 125, 150, 150));
CGContextBeginPath(ctx);
CGContextEOFillPath(ctx);
CGContextFillEllipseInRect(ctx, CGRectMake(100, 100, 200, 200));

1
它完全填满了圆形。 - Ka-rocks
请使用以下运行示例检查此答案:https://dev59.com/WEfRa4cB1Zd3GeqP7Da9#27796085 - Gabriel.Massana
1个回答

13

您需要的更像是这样:

- (void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextAddEllipseInRect(ctx, rect);
    CGContextAddEllipseInRect(ctx, 
                CGRectMake(
                    rect.origin.x + 10, 
                    rect.origin.y + 10, 
                    rect.size.width - 20, 
                    rect.size.height - 20));
    CGContextSetFillColor(ctx, CGColorGetComponents([[UIColor blueColor] CGColor]));
    CGContextEOFillPath(ctx);
}

这将在当前路径中添加两个省略号(其中一个比另一个小,但围绕着同一点居中)。当它填充路径时,EOFillPath基本上会从外部椭圆中“减去”内部椭圆。

如果要创建“同心圆”,如果这确实是您想要的,您可以为更多连续变小的椭圆重复此操作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接