2

我有以下代码:

    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];

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

4

2 回答 2

2

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

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

此方法在识别手势时调用,gestureRecognizer或者otherGestureRecognizer会阻止其他手势识别器识别其手势。注意返回YES保证允许同时识别;NO另一方面,返回不能保证防止同时识别,因为其他手势识别器的委托可能会返回YES

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

于 2012-12-19T04:45:46.423 回答
0

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

swipeGesture.requireGestureRecognizerToFail(panGesture)

如果目标是让滑动手势/秒阻止识别平移(不寻常的情况):

panGesture.requireGestureRecognizerToFail(swipeGesture)

也照常做:

panGesture.delegate = self
swipeGesture.delegate = self

并实现shouldRecognizeSimultaneouslyWithGestureRecognizer始终返回true

注意:UIWebView在's 和' 之类的东西上,UIScrollView要正确解释单点触摸滑动,最好使用以下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()
    }
}
于 2015-06-13T00:00:13.533 回答