Drawing a circle in UIView is similar to drawing a border which is shown in the previous post.
iPhone – UIView with Border
Please follow the above example and modify the UIViewWithBorder.m as below.
UIViewWithBorder.m
... - (void)drawRect:(CGRect)rect { /* Set UIView Border */ /* // Get the contextRef CGContextRef contextRef = UIGraphicsGetCurrentContext(); // Set the border width CGContextSetLineWidth(contextRef, 5.0); // Set the border color to RED CGContextSetRGBStrokeColor(contextRef, 255.0, 0.0, 0.0, 1.0); // Draw the border along the view edge CGContextStrokeRect(contextRef, rect); */ /* Draw a circle */ // Get the contextRef CGContextRef contextRef = UIGraphicsGetCurrentContext(); // Set the border width CGContextSetLineWidth(contextRef, 1.0); // Set the circle fill color to GREEN CGContextSetRGBFillColor(contextRef, 0.0, 255.0, 0.0, 1.0); // Set the cicle border color to BLUE CGContextSetRGBStrokeColor(contextRef, 0.0, 0.0, 255.0, 1.0); // Fill the circle with the fill color CGContextFillEllipseInRect(contextRef, rect); // Draw the circle border CGContextStrokeEllipseInRect(contextRef, rect); } ...
Reference: Draw Circle
Thank you, this is what I’m been looking for.
LikeLike
you are welcome =)
LikeLike
Thank you for share it, it’s a very nice example for beginners like me… now, can i give to this shape an action (like to a button) ??
LikeLike
you can take a look @ this post
StackOverflow – Touch Event on UIView
LikeLike
You’d better use CGContextAddArc() with startAngle=0 and endAngle=2*pi if you want a real round circle. My sample code:
LikeLike
thanks for your code. =)
LikeLike
thanks !!!!
LikeLike
CGContextSetRGBFillColor(context, 0x00 / 255.0, 0x00 / 255.0, 0xff / 255.0, 0xff / 255.0);
but not CGContextSetRGBStrokeColor(contextRef, 0.0, 0.0, 255.0, 1.0);
LikeLike
CGContextSetRGBStrokeColor() is for the border color while the CGContextSetRGBFillColor() is for the color inside the circle.
LikeLike