user979331 user979331 - 4 months ago 23
Objective-C Question

CGContextAddArc draw multiple circle at different sizes

I have these sizes that I want to draw a circle with (both width and height are the same obviously)

205.0
218.0
245.0
257.0
310.0
510.0


When I draw my circle at 205.5 its fine, its a perfect circle. But when I start with 218, the circle gets cut off a little and it gets more and more cut off each time I try to make a bigger circle. My question is, how do I create a circle that is perfect, no matter what the size ?

Here is my code:

func drawCircle()
{
// Get the Graphics Context
let context = UIGraphicsGetCurrentContext();

// Set the circle outerline-width
CGContextSetLineWidth(context, 5.0);

// Set the circle outerline-colour
UIColor.redColor().set()

// Create Circle
CGContextAddArc(context, (round(frame.size.width))/2, round(frame.size.height)/2, (round(frame.size.width) - 10)/2, 0.0, CGFloat(M_PI * 2.0), 1)

// Draw
CGContextStrokePath(context);
}


frame.size.width
and
frame.size.height
are the numbers of 205, 218 and so on. I will accept an Objective-C answer.

Answer

If you want to draw a circle, use CGContextAddEllipseInRect instead of CGContextAddArc.

let context = UIGraphicsGetCurrentContext()
CGContextSetLineWidth(context, 5.0)
UIColor.redColor().set()

CGContextAddEllipseInRect(context, frame)  // <-- 

CGContextStrokePath(context)