,而不是針對一個盒子測試,你可以對你想要的任何形狀當然測試。
我最初發布時,你可以使用一個NSBezierPath,但appearantly是ins't iPhone上可用工具包,只能在Mac上。相反,在iPhone上,您可以使用CGPath。
使用CGPathCreateMutable()
返回一個const CGPath *
(也稱爲CHPathRef
然後使用CGPathAddRect
或CGPathAddLines
來創建我們的路徑創建一個新路徑。
CGPathContainsPoint
將測試如果你的點是在形狀。
或者,您可以創建一個客戶的功能(因爲你使用三角形)不只是一個簡單的計算,以檢查您的點是否在三角形內部。數學的位應該做的伎倆(儘管當你旋轉的形狀,它會稍多複雜,我寫咯,你可以然後只需旋轉touc相對^ h點形狀的起源和做碰撞檢測)
對於一個三角形:
C
/\
/__\
A B
point of touch is P
以下算法,你應該能夠找到觸摸:
/* first test against a box, for performance */
if(P.y > C.y || P.y < A.y || P.x < A.x || P.X > B.x)
return false; // false if P falls outside "the box"
/* then check if its within the shape */
/* split the triangle into 2 parts, around the axle of point C */
if(P.x < C.x) // if the x value of point P is on the left of point C
if(P.y > ((C.y -A.y)/(C.x - A.x)) * P.x)
return false; // the point is above the triangle's side AC
else // if the x value of point P is greater than or equal to point C
if(P.y > C.y - ((C.y - B.y)/(B.x - C.x)) * (P.x - C.x))
return false; // the point is above the triangle's side BC
return true; // the point must be in the triangle, so return true.
以上是幹編碼的,但應該是正確的。
上面只能對一個三角形的形狀,我畫它(其中C.x是A.x和B.x,以及A和B之間處於相同的高度,但低於C)。當然你可以修改這個來測試任何形狀,但是,你必須權衡這個與使用可用的CGPath
。
如果您沒有得到它,或者如果它有問題,請告訴我!