2012-10-19 25 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