ios - 绘制同心圆: Inner circle should be filled

标签 ios core-graphics

我正在尝试实现一个形状为 enter image description here 的按钮

这是我正在尝试的当前代码。它实现了同心圆,但外面的那个被填充了。帮助。

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

最佳答案

制作两个上下文,只填充内圈

- (void)drawRect:(CGRect)rect
{
    CGContextRef outer = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(outer, 1.5);
    CGContextSetStrokeColorWithColor(outer, [UIColor blueColor].CGColor);
    CGContextAddEllipseInRect(outer, CGRectMake(rect.origin.x + 5, rect.origin.y + 5, rect.size.width - 10, rect.size.height - 10));
    CGContextStrokePath(outer);

    CGContextRef inner = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(inner, 1.5);
    CGContextSetFillColorWithColor(inner, [UIColor blueColor].CGColor);
    CGContextAddEllipseInRect(inner, CGRectMake(rect.origin.x + 15, rect.origin.y + 15, rect.size.width - 30, rect.size.height - 30));
    CGContextFillPath(inner);
}

我在外部上下文中添加了 5 个单位的填充,使其看起来像圆形而不是圆角矩形。

关于ios - 绘制同心圆: Inner circle should be filled,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22829494/

相关文章:

ios - UILabel 并不总是出现在它的 super View 中

iphone - 用阴影画线,但只想保留阴影。操作系统

macos - 在所见即所得 Mac 应用程序中布置用于打印的图像的最佳实践?

ios - 在 iOS 上显示 CVImageBufferRef 的最有效方法是什么

ios - 在 iOS 和 iPhone 上,如何绘制到位图并在 drawRect 中显示?

ios - 变换使尺寸不同

iphone - 来自 tableViewController 子类的 UINavigationController pushViewController

ios - 在 Swift 中执行 POST 请求

ios - 无法将 SKEffectNode 视为具有建议效果的 child

ios - 调用presentLocalNotificationNow 时触发didReceiveLocalNotification