我想检测从屏幕右下角到中间的两指对角滑动。我尝试添加 UISwipeGestureRecognizer,方向设置为“UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionLeft”,但没有任何效果,因为即使我从屏幕中间向左上角开始滑动,也会调用处理程序。
我是否需要对 UIGestureRecognizer 进行子类化,或者我可以使用 TouchBegan 和 TouchMoved 来处理此问题吗?
您需要将其子类化才能使用touchesBegan
and touchesMoved
反正。我会做UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionLeft
就像你说的,然后设置两个CGRect
s,一个用于可接受的起点,一个用于可接受的终点。然后使用UIGestureRecognizerDelegate
method gestureRecognizer:shouldReceiveTouch:
,并使用以下命令检查触摸点是否位于可接受的起始矩形中CGRectContainsPoint()
。然后(我认为)在你的UISwipeGestureRecognizer
子类,覆盖touchesEnded:withEvent:
,并检查结束点是否在可接受的矩形内。如果不是,则将状态设置为UIGestureRecognizerStateCancelled
(或者无论如何你应该取消一个手势)。还要确保它附加到的视图有它的multipleTouchEnabled
属性集。我实际上还没有尝试过这个,但是类似的东西应该可以做到。祝你好运!
EDIT
实际上,如果您不想担心可接受的起点/终点的特定矩形值,并使其与设备无关,您可以这样做:
//swap in whatever percentage of the screen's width/height you want in place of ".75f"
//set the origin for acceptable start points
CGFloat startRect_x = self.view.frame.size.width * .75f;
CGFloat startRect_y = self.view.frame.size.height * .75f;
//set the size
CGFloat rectWidth = self.view.frame.size.width - startRect_x;
CGFloat rectHeight = self.view.frame.size.height - startRect_y;
//make the acceptable start point rect
CGRect startRect = CGRectMake(startRect_x, startRect_y, rectWidth, rectHeight);
//set the origin for the accepable end points
CGFloat endRect_x = self.view.center.x - rectWidth/2;
CGFloat endRect_y = self.view.center.y - rectHeight/2;
//make the acceptable end point rect
CGRect endRect = CGRectMake(endRect_x, endRect_y, rectWidth, rectHeight);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)