2009-10-10 66 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