使用 Core Graphics 绘制路径时出现问题 - iPhone
我正在尝试在我的应用程序中绘制地图注释 - 非常类似于 MapKit 的 MKAnnotationView,但没有 Mapkit。
我对视图轮廓的路径排序有问题,我无法弄清楚。
结果图片:
http://img504.imageshack.us/img504/5458/screenshot20091010at703。 png
代码:
CGFloat minx = CGRectGetMinX(currentBounds);
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);
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);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,在 PostScript(以及 AppKit 绘图和 Core Graphics 等衍生产品)中,您通常会绘制逆时针路径,尤其是在填充时。如果您想在其外部填充,则需要绘制像此处所示的顺时针路径。
其次,我假设这个上下文起源于左上角(正 y 向下),而不是左下角(正 y 向上)。
从第一条弧线出来后,当前点位于指针底部的死点。将arct命令的第二个点(CGContextAddArcToPoint)设置为指针的第一个基点不是更有意义吗?除了更加正确之外,您只需要向 CGContextAddLines 传递两个点即可。
你的意思是关闭路径两次吗?我不认为这有什么坏处,但这是多余的。
我将开始在指针的右基点处绘制,然后绘制到接下来的两个点(尖端和左基点,按此顺序)的线,然后按(逆时针)连续绘制所有四个弧,然后
closepath
(一次)。这应该稍微简单一些,而且正确。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 toCGContextAddLines
.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.因此,无论输入什么值,无论是否有 CGContextPathBegin,CGContextMoveToPoint 都不起作用。用指针箭头角处的线开始路径允许我指定起点。感谢彼得帮助我理解它以及如何简化路径。
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.