手势方法(平移手势和滑动手势)之间是否有优先级条件?
我正在开发一个应用程序,其中使用了平移手势和滑动手势。因此,每次我执行“滑动手势”时,但总是会调用“平移手势”中的方法,而不会调用“滑动手势”方法。
所有手势方法之间有优先级吗?
I am developing an application where I have used the Pan Gesture as well as Swipe Gesture. So every time I do the Swipe Gesture but the method from the Pan Gesture is always getting called and Swipe Gesture method is not getting called.
Is there any priority between all the gesture method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过实现 UIGestureRecognizerDelegate 协议的以下方法来并行调用它们:
You can call them in parallel by implementing the following method of the
UIGestureRecognizerDelegate
protocol:UIGestureRecognizer
类上有一个名为“cancelsTouchesInView”的属性,默认为YES
。这将导致所有待处理的手势被取消。平移手势首先被识别,因为它不需要“触摸”事件,因此它取消了滑动手势。如果您希望两种手势都能被识别,请尝试添加:
[yourPanGestureInstance setCancelsTouchesInView:NO];
There is a property on the
UIGestureRecognizer
class called "cancelsTouchesInView" which defaults toYES
. This will cause any pending gestures to be canceled. The Pan gesture is getting recognized first since it does not need to have a "touch up" event, so it cancels the Swipe gesture.If you want both gestures to be recognized, try adding:
[yourPanGestureInstance setCancelsTouchesInView:NO];
优先滑动
您可以使用
require(toFail:)
方法优先考虑UIGestureRecognizer
。现在,仅当您的滑动失败时,您的平移才会执行。
对所有内容都使用平移
如果滑动和平移手势识别器在此设置中不能很好地发挥作用,您可以将所有逻辑滚动到平移手势识别器中以获得更多控制。
这是上述代码的实际演示。
Give priority to swipe
You can give priority to a
UIGestureRecognizer
with therequire(toFail:)
method.Now your pan will only execute if your swipe fails.
Use pan for everything
If the swipe and pan gesture recognizers don't play nicely with this setup, you can roll all of your logic into the pan gesture recognizer for more control.
Here's a demo of the above code in action.