如何使UISwipeGestureRecognizer和UIPanGestureRecognizer在同一视图上工作 [英] How to have a UISwipeGestureRecognizer AND UIPanGestureRecognizer work on the same view

查看:272
本文介绍了如何使UISwipeGestureRecognizer和UIPanGestureRecognizer在同一视图上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将如何设置手势识别器,以便同时使用 UISwipeGestureRecognizer UIPanGestureRecognizer ?这样,如果您触摸并快速移动(快速滑动),它将手势检测为滑动,但是如果您触摸然后移动(触摸与移动之间的短暂延迟),它将检测为手势?

How would you setup the gesture recognizers so that you could have a UISwipeGestureRecognizer and a UIPanGestureRecognizer work at the same time? Such that if you touch and move quickly (quick swipe) it detects the gesture as a swipe but if you touch then move (short delay between touch & move) it detects it as a pan?

我尝试了 requireGestureRecognizerToFail 的各种排列,但这并不能完全解决问题,因此,如果离开SwipeGesture,则我的摇摄手势可以上下左右移动,但任何移动都可以滑动手势即可检测到左侧.

I've tried various permutations of requireGestureRecognizerToFail and that didn't help exactly, it made it so that if the SwipeGesture was left then my pan gesture would work up, down and right but any movement left was detected by the swipe gesture.

推荐答案

您将需要将两个UIGestureRecognizer的委托之一设置为有意义的对象(可能是self),然后进行侦听,并为此YES "rel =" nofollow noreferrer>方法:

You're going to want to set one of the two UIGestureRecognizer's delegates to an object that makes sense (likely self) then listen, and return YES for this method:

- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
         shouldRecognizeSimultaneouslyWithGestureRecognizer:
                            (UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

gestureRecognizerotherGestureRecognizer识别手势会阻止另一个手势识别器识别其手势时,将调用此方法.请注意,返回YES可以保证同时识别.另一方面,不能保证返回NO会阻止同时识别,因为另一个手势识别器的委托人可能会返回YES.

This method is called when recognition of a gesture by either gestureRecognizer or otherGestureRecognizer would block the other gesture recognizer from recognizing its gesture. Note that returning YES is guaranteed to allow simultaneous recognition; returning NO, on the other hand, is not guaranteed to prevent simultaneous recognition because the other gesture recognizer's delegate may return YES.

这篇关于如何使UISwipeGestureRecognizer和UIPanGestureRecognizer在同一视图上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆