iOS 绘制填充圆圈
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17038017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS draw filled Circles
提问by RegularExpression
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.
不是这里的图形程序员,所以我试图通过这个绊倒。我正在尝试绘制 9 个实心圆,每个圆都有不同的颜色,每个圆都有白色边框。UIView 的框架是 CGRectMake (0,0,60,60)。见附图。
The problem is I'm getting "flat spots" on the borders on each side. Following is my code (from the UIView subclass):
问题是我在每一边的边界上都有“平坦点”。以下是我的代码(来自 UIView 子类):
- (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);
}
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.
如果我在 drawRect 中更改为 CGRectMake(0,0,56,56),我只会在顶部和左侧得到平坦的点,而底部和右侧看起来很好。
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.
谁能建议我如何解决这个问题?在我看来,边框正在被 UIView 裁剪,但对此知之甚少,我真的不知道如何修复它。
Thanks, in advance, for any of you graphics experts' suggestions.
在此先感谢各位图形专家的建议。
回答by Wain
I like the answer from @AaronGolden, just wanted to add:
我喜欢@AaronGolden 的回答,只想补充:
CGRect borderRect = CGRectInset(rect, 2, 2);
Or, better:
或更好:
CGFloat lineWidth = 2;
CGRect borderRect = CGRectInset(rect, lineWidth * 0.5, lineWidth * 0.5);
回答by Aaron Golden
Those circles are just getting clipped to the bounds of the views that draw them. The views must be slightly larger than the circles to be drawn. You can imagine the CGContextStrokeEllipseInRect
call tracing a circle of radius 30 and then painting one pixel on each side of the traced curve. Well on the far edges you're going to have one of those pixels just outside the boundary of the view.
这些圆圈只是被裁剪到绘制它们的视图的边界上。视图必须比要绘制的圆圈稍大。您可以想象CGContextStrokeEllipseInRect
调用跟踪半径为 30 的圆,然后在跟踪曲线的每一侧绘制一个像素。那么在远边缘,您将在视图边界之外拥有这些像素之一。
Try making your views something like 62x62, or make the circle radius slightly smaller to leave room for the thick stroke in your 60x60 views.
尝试将视图设置为 62x62 之类的大小,或者将圆半径稍微小一点,以便为 60x60 视图中的粗笔画留出空间。
回答by Trianna Brannon
I Wrote this so that you can draw many circles easily.
我写这个是为了你可以很容易地画很多圆圈。
Add the following code to your .m file:
将以下代码添加到您的 .m 文件中:
- (void) circleFilledWithOutline:(UIView*)circleView fillColor:(UIColor*)fillColor outlineColor:(UIColor*)outlinecolor{
CAShapeLayer *circleLayer = [CAShapeLayer layer];
float width = circleView.frame.size.width;
float height = circleView.frame.size.height;
[circleLayer setBounds:CGRectMake(2.0f, 2.0f, width-2.0f, height-2.0f)];
[circleLayer setPosition:CGPointMake(width/2, height/2)];
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(2.0f, 2.0f, width-2.0f, height-2.0f)];
[circleLayer setPath:[path CGPath]];
[circleLayer setFillColor:fillColor.CGColor];
[circleLayer setStrokeColor:outlinecolor.CGColor];
[circleLayer setLineWidth:2.0f];
[[circleView layer] addSublayer:circleLayer];
}
Then Add the following code to your view did load and replace "yourView" with any view that you want to place the circle in. If you want to make a bunch of circles just add some small views to the page and repeat the code below. The circle will become the size of the view you make.
然后将以下代码添加到您的视图中并加载并将“yourView”替换为您想要放置圆圈的任何视图。如果您想制作一堆圆圈,只需向页面添加一些小视图并重复下面的代码。圆圈将成为您创建的视图的大小。
[self circleFilledWithOutline:self.yourView fillColor:[UIColor redColor] outlineColor:[UIColor purpleColor]];
回答by u4416697
There is a simple way to draw a fill circle
有一个简单的方法来绘制一个填充圆
CGContextFillEllipseInRect(context, CGRectMake(x,y,width,height))
回答by user7205499
Trianna Brannon's answer on Swift 3
Trianna Brannon 对Swift 3的回答
func circleFilledWithOutline(circleView: UIView, fillColor: UIColor, outlineColor:UIColor) {
let circleLayer = CAShapeLayer()
let width = Double(circleView.bounds.size.width);
let height = Double(circleView.bounds.size.height);
circleLayer.bounds = CGRect(x: 2.0, y: 2.0, width: width-2.0, height: height-2.0)
circleLayer.position = CGPoint(x: width/2, y: height/2);
let rect = CGRect(x: 2.0, y: 2.0, width: width-2.0, height: height-2.0)
let path = UIBezierPath.init(ovalIn: rect)
circleLayer.path = path.cgPath
circleLayer.fillColor = fillColor.cgColor
circleLayer.strokeColor = outlineColor.cgColor
circleLayer.lineWidth = 2.0
circleView.layer.addSublayer(circleLayer)
}
And invoke func via:
并通过以下方式调用 func:
self.circleFilledWithOutline(circleView: myCircleView, fillColor: UIColor.red, outlineColor: UIColor.blue)