代码之家  ›  专栏  ›  技术社区  ›  dizy

如何检测特定区域上的触摸

  •  6
  • dizy  · 技术社区  · 16 年前

    目前,我看到一个触摸事件将向我显示发生触摸的UIView。但是如果我需要检测一些非矩形的形状,比如一个圆,会怎么样呢。我该怎么做那样的事呢?

    基本上,我只想在用户触摸不可见的圆形区域内的某个地方时做一些事情。

    感谢您的帮助/指导,TIA!

    1 回复  |  直到 16 年前
        1
  •  7
  •   Andrew Grant    16 年前

    你会这样做的。请注意,“locationInView”将返回相对于指定视图的触摸坐标,因此,无论视图在屏幕上的何处,视图左上角的触摸都将返回(0,0)。

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    {   
      UITouch *touch = [touches anyObject];
    
      // gets the coordinats of the touch with respect to the specified view. 
      CGPoint touchPoint = [touch locationInView:self];
    
      // test the coordinates however you wish, 
      ...
    }
    

    要针对球体进行测试,您需要计算从接触点到球体中心的距离,然后检查该距离是否小于球体半径。