问题描述
目前我看到触摸事件会向我显示触摸发生的UIView。但是,如果我需要检测一些非矩形形状的触摸,如圆形。我该如何做这样的事情?
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.
感谢任何帮助/方向,TIA!
Any help/direction is appreciated, TIA!
推荐答案
你会这样做。请注意,'locationInView'将返回相对于指定视图的触摸坐标,因此无论视图在屏幕上的哪个位置,视图左上角的触摸都将返回(0,0)。
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.
这篇关于如何检测特定区域的触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!