UISwipeGestureRecognizer和UIPanGestureRecognizer在一个视图中出现问题

时间:2012-12-19 04:41:51

标签: iphone objective-c ios ipad

我有以下代码:

    UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHighlightReadingVC:)];
    swipeGestureRecognizer.delegate = self;
    [self.highlightReadingVC_.view addGestureRecognizer:swipeGestureRecognizer];

    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panHighlightReadingVC:)];
    panGestureRecognizer.delegate = self;
    [panGestureRecognizer requireGestureRecognizerToFail:swipeGestureRecognizer];
    [self.highlightReadingVC_.view addGestureRecognizer:panGestureRecognizer];

我的问题是,虽然我在视图上滑动,但它总是首先检测到平移手势。我希望在平移/拖动之前首先识别滑动。我该怎么做?

2 个答案:

答案 0 :(得分:2)

您将要将两个UIGestureRecognizer的委托之一设置为有意义的对象(可能self)然后侦听,然后返回YES

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

gestureRecognizerotherGestureRecognizer识别手势会阻止其他手势识别器识别其手势时,会调用此方法。请注意,保证返回YES可以同时识别;另一方面,返回NO并不能保证阻止同时识别,因为其他手势识别器的代表可能会返回YES

我在我的应用程序中也遇到了同样的问题。这对我来说可以。所以它可以解决你的问题。

答案 1 :(得分:0)

如果目标是平移手势/ s阻止识别滑动(通常情况):

swipeGesture.requireGestureRecognizerToFail(panGesture)

如果目标是滑动,则阻止识别平移(不寻常的情况):

panGesture.requireGestureRecognizerToFail(swipeGesture)

也按常规做法:

panGesture.delegate = self
swipeGesture.delegate = self

并实施shouldRecognizeSimultaneouslyWithGestureRecognizer以始终返回true

注意:UIWebViewUIScrollView之类的内容上正确解释单触式滑动,最好requireGestureRecognizerToFail全部三个( 3):平移,长按和第二个滑动识别器.numberOfTouchesRequired = 2,使UX能够直观地工作。

这是我们使用的(Swift):

class AboutViewController: UIViewController, UIGestureRecognizerDelegate {

    @IBOutlet var webView: UIWebView!

    var swipeToDismiss: UISwipeGestureRecognizer {
        var r = UISwipeGestureRecognizer(target: self, action: "dismiss:")
        r.direction = .Right
        r.delegate = self
        r.requireGestureRecognizerToFail(longPress)
        r.requireGestureRecognizerToFail(pan)
        r.requireGestureRecognizerToFail(doubleSwipe)
        return r
    }

    var doubleSwipe: UISwipeGestureRecognizer {
        var r = UISwipeGestureRecognizer()
        r.direction = .Right
        r.delegate = self
        r.numberOfTouchesRequired = 2
        return r
    }

    var longPress: UILongPressGestureRecognizer {
        var r = UILongPressGestureRecognizer()
        r.allowableMovement = 4000.0
        r.delegate = self
        return r
    }

    var pan: UIPanGestureRecognizer {
        var r = UIPanGestureRecognizer()
        r.delegate = self
        return r
    }

    func dismiss(recognizer: UISwipeGestureRecognizer!) {
         // ...popViewControllerAnimated(true)
    }

    private func setupWebView() {
        // ...
        webView.addGestureRecognizer(longPress)
        webView.addGestureRecognizer(swipeToDismiss)
        webView.addGestureRecognizer(pan)
        webView.addGestureRecognizer(doubleSwipe)
    }


    // MARK: - UIGestureRecognizerDelegate

    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        println("gesture start: \(gestureRecognizer)")
        return true
    }

    func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        println("multiple gestures: \(gestureRecognizer), \(otherGestureRecognizer)")
        return true
    }


    // MARK: - UIViewController

    override func viewDidLoad() {
        super.viewDidLoad()

        setupWebView()
    }
}