2012-10-19 27 views
2

有沒有辦法知道'tap'是在UIView的蒙面區域內部還是外部?我正在使用CoreGraphics來掩蓋UIView。UIGestureRecogniser在蒙面UIView上?

Tap Location Diagram

到目前爲止,我的代碼是這樣的..

- (void)viewDidLoad { 

    UIGestureRecogniser *r = [[UIGestureRecogniser alloc] initWithTarget:self action:@selector(gestCall:)]; 
    [self addGestureRecogniser:r]; 

} 

- (void)gestCall:(UIGestureRecogniser *)gestRec { 
    if ("somthing") { 
     // outside of mask 
    } else { 
     // inside of mask 
    } 
} 

謝謝。

回答

4

我終於找到了我正在尋找的解決方案。因此,爲了任何人試圖找到的好處,CGPoint在任何CGPath中。

很簡單。

UIBezierPath *p = [UIBezierPath bezierPathWithCGPath:anyCGPath]; 

BOOL isInPath = [p containsPoint:anyCGPoint]; 
2

基本上你需要檢查觸摸座標並決定是否落入遮罩區域。覆蓋hitTest:withEvent:並考慮圖像蒙版。您可以在覆蓋的` - [UIView hitTest:withEvent:]中使用[[[self layer] presentationLayer] hitTest:aPoint][[[self layer] mask] hitTest:aPoint]

[編輯]

Check if a user tapped near a CGPath可能有助於找到問題的答案。

[編輯]

不要在你的手勢處理機下面找出處理自來水或沒有。

  1. 指定圓的中心(這將是作爲UIView.Center CGPoint)
  2. 指定餅圖的半徑
  3. 當在查看用戶抽頭,得到該位置作爲點 - CGPoint和計算point.x*point.x+point.y*point.y(圓公式),並且此值必須小於或等於半徑的平方,即radius*radius。如果這個條件滿足,那麼你的抽頭點在圓外,否則在外面。

希望明確。

+0

澄清我想知道CGPoint是否在CGPath內。我認爲這可能是一個簡單的方法。我對Core Graphics沒有經驗。 –

+0

好的。你想要達到什麼目的?你想要哪種手勢?它只是單擊?你的面具總是呈圓形嗎? – applefreak

+0

檢查我發佈的SO鏈接。你可能需要鍛鍊一點。 – applefreak