Problem with drawing paths with Core Graphics - iPhone
I am trying to draw a map annotation in my app - very much like MapKit's MKAnnotationView, but without the mapkit.
I have a problem with the ordering of the path for the view outline that I cant figure out.
Image of results:
http://img504.imageshack.us/img504/5458/screenshot20091010at703.png
Code:
CGFloat minx = CGRectGetMinX(currentBounds);开发者_C百科
CGFloat midx = CGRectGetMidX(currentBounds);
CGFloat maxx = CGRectGetMaxX(currentBounds);
CGFloat miny = CGRectGetMinY(currentBounds)+10.0f;
CGFloat midy = CGRectGetMidY(currentBounds)+10.0f;
CGFloat maxy = CGRectGetMaxY(currentBounds)+10.0f;
CGContextBeginPath(currentContext);
CGContextMoveToPoint(currentContext, minx, miny+radius); //before top left arc
CGContextAddArcToPoint(currentContext, minx, miny, midx, miny, radius); //top left
CGPoint points1[] = {
CGPointMake(midx-10.0f, miny),
CGPointMake(midx, 0.0f), //tip of arrow
CGPointMake(midx+10.0f, miny),
};
CGContextAddLines(currentContext, points1, 3);
CGContextAddArcToPoint(currentContext, maxx, miny, maxx, midy, radius); //top right
CGContextAddArcToPoint(currentContext, maxx, maxy, midx, maxy, radius); //bottom right
CGContextAddArcToPoint(currentContext, minx, maxy, minx, midy, radius); //bottom left
CGContextClosePath(currentContext);
CGContextClosePath(currentContext);
CGContextDrawPath(currentContext, kCGPathFillStroke);
//CGContextDrawPath(currentContext, kCGPathEOFillStroke);
First, in PostScript (and derivatives such as AppKit drawing and Core Graphics), you normally draw a counter-clockwise path, especially when filling. A clockwise path like the one you show here is what you'd draw if you want to fill outside it.
Second, I'm assuming that this context has origin in the top-left (positive y going down), not the bottom-left (positive y going up).
Coming out of the first arc, the current point is dead-center at the bottom of the pointer. Would it not make more sense to set the second point of the arct
command (CGContextAddArcToPoint
) as the first base point of the pointer? Besides being more correct, you would only need to pass two points to CGContextAddLines
.
Do you mean to close the path twice? I don't think it hurts anything, but it is redundant.
I would start drawing at the right base point of the pointer, then plot the lines to the next two points (tip and left base point, in that order), then plot all four arcs in (counterclockwise) succession, then closepath
(once). That should be slightly simpler, and correct.
So the CGContextMoveToPoint was not working, no matter what values were entered, with or without the CGContextPathBegin. Starting the path with the line at the corner of the pointer arrow allowed me to specify starting point. Thanks Peter for helping me understand it, and how to simplify the paths.
精彩评论