iOS绘制填充圈子

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

问题是我在两边的边界上得到“平坦的斑点”。 以下是我的代码(来自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); } 

如果我在drawRect中更改为CGRectMake(0,0,56,56),我只在顶部和左侧得到平坦的点,并且底部和右侧看起来很好。

任何人都可以build议如何解决这个问题? 在我看来,边界正在被UIView截取,但是对此并不了解,我真的不知道如何解决这个问题。

提前感谢您的任何graphics专家的build议。

在这里输入图像说明

我喜欢@AaronGolden的答案,只是想补充一下:

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

或更好:

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

那些圈子正在被剪裁到绘制它们的视图的边界上。 视图必须比要绘制的圆圈稍大。 您可以想象CGContextStrokeEllipseInRect调用跟踪半径为30的圆,然后在跟踪曲线的每一边绘制一个像素。 那么在远处边缘你将有一个像素在视图的边界之外。

尝试使您的视图像62×62一样,或者使圆的半径稍微缩小,以便在60×60的视图中留下厚的中风的空间。

我写了这个,这样你可以很容易地画出很多圈子。

将下面的代码添加到.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]; } 

然后将下面的代码添加到您的视图中,并将视图replace为您想放置该圆的视图。如果要制作一堆圆形,只需在页面中添加一些小视图,然后重复下面的代码即可。 圆圈将成为你所做的视图的大小。

 [self circleFilledWithOutline:self.yourView fillColor:[UIColor redColor] outlineColor:[UIColor purpleColor]]; 

有一个简单的方法来绘制填充圆

 CGContextFillEllipseInRect(context, CGRectMake(x,y,width,height)) 

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

并通过以下方式调用func:

 self.circleFilledWithOutline(circleView: myCircleView, fillColor: UIColor.red, outlineColor: UIColor.blue)