2013-08-07 52 views
2

我無法在使用UIBezierpath繪製的線中找到觸摸點。 CGPathContainPoint不適用於線路。請幫助我在一行中檢測觸摸點

+0

http://stackoverflow.com/questions/10831370/how-to-check-if-touch-point-is-on-uibezierpath-ios可以通過這個鏈接 – Jitendra

回答

7

您可以創建另一個路徑對象,表示路徑實際在屏幕上的顯示方式,然後檢查觸摸點是否位於該路徑內。在CGPath reference中,您可以找到方便的構造函數CGPathCreateCopyByStrokingPath。你可能會有點像這樣使用:

CGPathRef originalPath = myBezierPath.CGPath; //The single-line path 

//Use the values you use to draw the path onscreen, 
//or use a width representing how far the user can touch 
//for it to be recognized by the path. 
//For example, for an error tolerance of 4px, use a width of 8px. 
CGPathRef strokedPath = CGPathCreateCopyByStrokingPath(originalPath, NULL, lineWidth, lineCap, lineJoin, miterLimit); 
BOOL pathContainsPoint = CGPathContainsPoint(strokedPath, NULL, touchPoint, NO); 

如上圖所示,這給你指定區域的利益,爲用戶而不是觸摸線。

希望這會有所幫助!

+1

我想從一組特定的行形狀 – Shruthi

+0

有什麼問題?這段代碼只是確定一個點是否與一條路徑相交。要從一組路徑中選擇一行,只需循環遍歷路徑併爲其中的每個路徑運行此代碼即可。 – architectpianist

+0

@architectpianist,如果我可以給這10個upvotes我會。我一直在尋找一種方法來檢測一個點落在一條線上,這個解決方案完美地工作。謝謝 – sbru