2009-10-10 81 views
0

我想在我的应用程序中绘制地图注释 - 非常像MapKit的MKAnnotationView,但没有mapkit。与核心图形绘制路径的问题 - iPhone

我有一个视图轮廓的路径排序问题,我不知道。结果

图片:

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); 

回答

0

所以无论CGContextMoveToPoint输入的是什么值,CGContextMoveToPoint都不起作用,无论是否使用CGContextPathBegin。用指针箭头的拐角处的线开始路径允许我指定起始点。感谢Peter帮助我理解它,以及如何简化路径。

+1

这并不是因为CGContextAddToLines做了一个隐式的'moveto'来取消你的显式的操作,所以它不起作用。在我的回答中查看我的最新评论。 – 2009-10-10 21:33:08

4

首先,在的PostScript(和其衍生物如了AppKit绘图和核心图形)时,通常绘制逆时针路径,尤其是在灌装时。像你在这里展示的一条顺时针的路径是你要画以外的画。第二,我假设这个背景起源于左上角(正y向下),而不是左下(正y向上)。

从第一个圆弧出来,当前点位于指针底部的死点。将arct命令的第二个点(CGContextAddArcToPoint)设置为指针的第一个基点是否更有意义?除了更加正确之外,您只需要通过两点即可获得CGContextAddLines

你的意思是关闭两次路径?我不认为它会伤害任何东西,但它是多余的。我将在指针的右基点开始绘制,然后将这些线绘制到接下来的两个点(以该顺序为基本点和左基点),然后在(逆时针)连续绘制所有四个弧,然后绘制closepath(一次)。这应该稍微简单一些,并且是正确的。

+0

CGContextMoveToPoint(currentContext,midx + 10.0f,miny); \t CGPoint点[] = { \t \t CGPointMake(MIDX,0.0F),//点 \t \t CGPointMake(MIDX-10.0f,MINY) \t}; \t CGContextAddLines(currentContext,points,2); \t \t CGContextAddArcToPoint(currentContext,minx,miny,minx,midy,radius); // TL \t CGContextAddArcToPoint(currentContext,minx,maxy,midx,maxy,radius); // BL \t CGContextAddArcToPoint(currentContext,maxx,maxy,maxx,midy,radius); // BR \t CGContextAddArcToPoint(currentContext,maxx,miny,midx,miny,radius); // TR \t *封闭通道,画* http://img36.imageshack.us/img36/92/screenshot20091010at816.png – Mobs 2009-10-10 09:17:05

+0

彼得,感谢提示..借口多么短暂该评论是 - 有一个人物限制。现在看来,起点不起作用? – Mobs 2009-10-10 09:18:09

+0

该代码绝对应该按预期工作。我建议提交关于closepath返回到第一行而不是moveto的错误。 https://bugreport.apple.com/ – 2009-10-10 11:23:32