使用 Core Graphics 绘制路径时出现问题 - iPhone

发布于 2024-08-07 12:57:14 字数 1446 浏览 7 评论 0原文

我正在尝试在我的应用程序中绘制地图注释 - 非常类似于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如日中天 2024-08-14 12:57:14

首先,在 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 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.

缘字诀 2024-08-14 12:57:14

因此,无论输入什么值,无论是否有 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文