Re: CGContext - Drawing Circle
Re: CGContext - Drawing Circle
- Subject: Re: CGContext - Drawing Circle
- From: Ken Thomases <email@hidden>
- Date: Thu, 04 Jul 2013 07:17:12 -0500
On Jul 4, 2013, at 6:44 AM, Dave wrote:
> I have a custom class, subclassed from UIControl that draws a circle with an X in it (like a circular check box control). This works ok, but the circle looks scrappy when displayed on the iPad. The method that does the work is appended to this mail and is called from the drawRect method of the class.
>
> Is there something I am doing wrong? It really doesn't look like a nice circle!
You don't really explain how it's not nice.
> myCenterXPosition = theDrawRect.origin.x + (theDrawRect.size.width / 2);
> myCenterYPosition = theDrawRect.origin.y + (theDrawRect.size.height / 2);
> myRadius = theDrawRect.size.width / 2;
>
> CGContextSetLineWidth(myGraphicsContext,2);
> CGContextAddArc(myGraphicsContext,myCenterXPosition,myCenterYPosition,myRadius,0,2 * M_PI,1);
Is theDrawRect coincident with the view's bounds? If so, then the edges of your circle will be clipped.
A stroked path straddles the imaginary zero-width path described by the geometry (a circle in this case). Since your line width is 2 points, then there's 1 point of width to the inside of the circle and 1 point of width to the outside. However, if the circle hits your view's bounds, then the portion outside of the circle is outside of the view and is not drawn. It's clipped to the view's bounds.
You might want to inset the circle by a point, which is to say reduce the radius by 1.
> CGContextStrokePath(myGraphicsContext);
Earlier you set the fill color, but you're not using it. You might consider using CGContextDrawPath(myGraphicsContext, kCGPathFillStroke) instead. That's not necessary if the view or window has already been cleared to a known background color.
> if (self.pCheckBoxSelectedFlag == YES)
> {
> myInnerRect = CGRectMake(CGRectGetMinX(theDrawRect) + 6,CGRectGetMinY(theDrawRect) + 6,CGRectGetWidth(theDrawRect) - 12,CGRectGetHeight(theDrawRect) - 12);
You want CGRectInset() here.
> CGContextSetLineWidth(myGraphicsContext,3);
>
> CGContextBeginPath(myGraphicsContext);
> CGContextMoveToPoint(myGraphicsContext,CGRectGetMinX(myInnerRect),CGRectGetMinY(myInnerRect));
> CGContextAddLineToPoint(myGraphicsContext,CGRectGetMaxX(myInnerRect),CGRectGetMaxY(myInnerRect));
> CGContextStrokePath(myGraphicsContext);
>
> CGContextMoveToPoint(myGraphicsContext,CGRectGetMinX(myInnerRect),CGRectGetMaxY(myInnerRect));
> CGContextAddLineToPoint(myGraphicsContext,CGRectGetMaxX(myInnerRect),CGRectGetMinY(myInnerRect));
> CGContextStrokePath(myGraphicsContext);
> }
> }
Regards,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden