How do I detect a touch over a specific area

后端 未结 1 990
孤独总比滥情好
孤独总比滥情好 2021-02-02 03:44

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

1条回答
  •  遥遥无期
    2021-02-02 04:31

    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.

    0 讨论(0)
提交回复
热议问题