iOS绘制填充圆圈 [英] iOS draw filled Circles

查看:460
本文介绍了iOS绘制填充圆圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里不是图形程序员,所以我试图绊倒这个。我正在尝试绘制9个实心圆圈,每个圆圈都有不同的颜色,每个圆圈都有一个白色边框。 UIView的框架是CGRectMake(0,0,60,60)。见附图。

Not a graphics programmer here, so I'm trying to stumble through this. I'm trying to draw 9 filled circles, each a different color, each with a white border. The UIView's frame is CGRectMake (0,0,60,60). See attached image.

问题是我在每边的边界上都有扁平斑点。以下是我的代码(来自UIView子类):

The problem is I'm getting "flat spots" on the borders on each side. Following is my code (from the UIView subclass):

- (void)drawRect:(CGRect)rect
{
    CGRect borderRect = CGRectMake(0.0, 0.0, 60.0, 60.0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
    CGContextSetRGBFillColor(context, colorRed, colorGreen, colorBlue, 1.0);
    CGContextSetLineWidth(context, 2.0);
    CGContextFillEllipseInRect (context, borderRect);
    CGContextStrokeEllipseInRect(context, borderRect);
    CGContextFillPath(context);
}

如果我在drawRect中更改为CGRectMake(0,0,56,56) ,我只在顶部和左侧,底部和底部都有扁平斑点。右边看起来很好。

If I change to CGRectMake(0,0,56,56) in drawRect, I get flat spots only on the top and left sides, and the bottom & right sides look fine.

有谁能建议我如何解决这个问题?在我看来,UIView正在修剪边框,但对此并不了解,我真的不知道如何修复它。

Can anyone suggest how I might fix this? It seems to me the border is being clipped by the UIView, but not knowing much about this, I really don't know how to fix it.

谢谢,提前,对于任何图形专家的建议。

Thanks, in advance, for any of you graphics experts' suggestions.

推荐答案

我喜欢@AaronGolden的答案,只想添加:

I like the answer from @AaronGolden, just wanted to add:

CGRect borderRect = CGRectInset(rect, 2, 2);

或者更好:

CGFloat lineWidth = 2;
CGRect borderRect = CGRectInset(rect, lineWidth * 0.5, lineWidth * 0.5);

这篇关于iOS绘制填充圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆