How do I detect a touch over a specific area

无人久伴 提交于 2020-01-22 05:32:05

问题


Currently I see that a touch event will show me the UIView where the touch occured. But what if I need to detect a touch of some non rectangular shape, like a circle. How would I go about doing something like that ?

Basically I want to do something only if the user touches somewhere within a circular area that's not visible.

Any help/direction is appreciated, TIA!


回答1:


You would do it like so. Note that 'locationInView' will return the coordinates of the touch with respect to the specified view, so a touch in the top-left corner of a view will return (0,0) regardless of where that view is onscreen.

-(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, 
  ...
}

To test against a sphere you would calculate the distance from the touch point to the center of the sphere, then check whether this was less than the sphere radius.



来源:https://stackoverflow.com/questions/647122/how-do-i-detect-a-touch-over-a-specific-area

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!