如何检测特定区域的触摸

diz*_*izy 6 iphone objective-c uikit

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

基本上我只想在用户触摸不可见的圆形区域内的某个地方时才做某事.

任何帮助/方向表示赞赏,TIA!

And*_*ant 7

你会这样做的.请注意,'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, 
  ...
}
Run Code Online (Sandbox Code Playgroud)

要测试球体,您将计算从触摸点到球体中心的距离,然后检查这是否小于球体半径.