为滚动视图上的双指滑动指定唯一行为

时间:2016-03-13 11:31:48

标签: ios swift scrollview uigesturerecognizer uiswipegesturerecognizer

我想在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)

        }
    }

1 个答案:

答案 0 :(得分:0)

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

首先,将self设置为识别器的委托。

doubleSwipeGestureRecognizer.delegate = self

其次,您在自己的类中编写了一个UIGestureRecognizerDelegate方法。

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! {
…
}