2

I would like to assign a unique behaviour for two-finger swipe on WKWebView. Referring to this site, I wrote codes as below. It worked, but scrolling on webview got extremely slow.

Is there any better way to do it by avoiding slow scrolling?

    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)

        }
    }
Yuichi Kato
  • 863
  • 7
  • 16

1 Answers1

0

Perhaps this codes suits for your demand.

First, you set self to delegate of recognizer.

doubleSwipeGestureRecognizer.delegate = self

Second, you writes a method of UIGestureRecognizerDelegate in self class.

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

    return false
}

Third, you delete 'for' sentence.

for gesture in self.webView!.scrollView.gestureRecognizers! {
…
}
r.izumita
  • 450
  • 1
  • 4
  • 13