为滚动视图上的双指滑动分配独特的行为

Assign unique behaviour for two-finger swipe on scrollview

我想为 WKWebView 上的双指滑动分配一个独特的行为。 参考 this site, 我写了如下代码。它有效,但在 webview 上滚动变得非常慢。

有没有避免缓慢滚动的更好方法?

    let doubleSwipeGestureRecognizer = UISwipeGestureRecognizer.init(target: self, action: "doubleSwiped2")
    doubleSwipeGestureRecognizer.numberOfTouchesRequired = 2
    doubleSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirection.Up
    self.webView!.addGestureRecognizer(doubleSwipeGestureRecognizer)

    for gesture in self.webView!.scrollView.gestureRecognizers!{
        let gestureClass = gesture.classForCoder
        let gestureName = NSStringFromClass(gestureClass)
        print(gestureName)

        if gestureName.containsString("Swipe"){
            // do nothing
        } else {
            gesture.requireGestureRecognizerToFail(doubleSwipeGestureRecognizer)

        }
    }

也许这段代码适合您的需求。

首先,您将自己设置为识别器的委托。

doubleSwipeGestureRecognizer.delegate = self

其次,你自己写了一个UIGestureRecognizerDelegate的方法class。

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailByGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    if otherGestureRecognizer is UIPanGestureRecognizer && otherGestureRecognizer.numberOfTouches() == 2 {
        return true
    }

    return false
}

第三,你删'for'句。

for gesture in self.webView!.scrollView.gestureRecognizers! {
…
}