2

I'm trying to implement Gesture recognizer over UIWebView to detect double taps. But there is no recognition.

I have looked over web for a few tutorials and I could compile to this. Code below. But there is no success.

class PageContentViewController: UIViewController, UIGestureRecognizerDelegate, UIWebViewDelegate  {

@IBOutlet weak var webView: UIWebView!

var pageIndex: Int = 0
var strTitle: String!
var flag : Int = 0

override func viewDidLoad() {
    super.viewDidLoad()
    webView.delegate = self

    DispatchQueue.main.async {
    let req = URLRequest(url: URL(fileURLWithPath: Bundle.main.path(forResource: self.strTitle+"/index" , ofType: "html")!))
    self.webView.loadRequest(req)
    self.view.addSubview(self.webView)

    }

    let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:#selector(self.goToback))
    tapGestureRecognizer.delegate? = self
    tapGestureRecognizer.numberOfTapsRequired = 2
    webView.isUserInteractionEnabled = true
    webView.addGestureRecognizer(tapGestureRecognizer)
    webView.scrollView.addGestureRecognizer(tapGestureRecognizer)
    webView.gestureRecognizerShouldBegin(tapGestureRecognizer)
}


func gestureRecognizer(_: UIGestureRecognizer,  shouldRecognizeSimultaneouslyWith:UIGestureRecognizer) -> Bool {
    return true
}
func gestureRecognizer(_: UIGestureRecognizer, shouldReceive:UITouch) -> Bool {
    return true
}


func goToback(){
    print("On the Back")
}

}

This picture contains webview in orange Attribute Inspector for UIWebView

What is wrong in the code.?

I'm noob to swift.

Thanks in advance

Prateekro
  • 566
  • 1
  • 6
  • 28

4 Answers4

2

I think you will need to add

webView.delegate = self

Also add UIWebViewDelegate to your class

Edit:

Your shouldRecognizeSimultaneouslyWith and shouldReceive look a bit weird maybe a copy paste mistake? It should look like:

func gestureRecognizer(_: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith:UIGestureRecognizer) -> Bool {
    return true
}
func gestureRecognizer(_: UIGestureRecognizer, shouldReceive:UITouch) -> Bool {
    return true
}

Edit 2:

I copied your exact code and removed gestureRecognizerShould begin and it worked, I was able to remove some other code as well, not sure if you needed it in your project but here is what works with me:

class ViewController: UIViewController, UIGestureRecognizerDelegate, UIWebViewDelegate  {

@IBOutlet weak var webView: UIWebView!

override func viewDidLoad() {
    super.viewDidLoad()
    webView.delegate = self

    DispatchQueue.main.async {
        let req = URLRequest(url: URL(fileURLWithPath: Bundle.main.path(forResource: self.strTitle+"/index" , ofType: "html")!))
        self.webView.loadRequest(req)
    }

    let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:#selector(self.goToback))
    tapGestureRecognizer.delegate = self
    tapGestureRecognizer.numberOfTapsRequired = 2
    webView.isUserInteractionEnabled = true
    webView.addGestureRecognizer(tapGestureRecognizer)
}


func gestureRecognizer(_: UIGestureRecognizer,  shouldRecognizeSimultaneouslyWith:UIGestureRecognizer) -> Bool {
    return true
}


func goToback() {
    print("On the Back")
}
Casper Zandbergen
  • 3,419
  • 2
  • 25
  • 49
1

Why are you using same gesture UIGestureRecognizer for webview and webview.scrollview.

Instead you need to create two separate UIGestureRecognizer for both like this...

// for webView
let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:#selector(self.goToback))
tapGestureRecognizer.delegate? = self
tapGestureRecognizer.numberOfTapsRequired = 2
webView.isUserInteractionEnabled = true
webView.addGestureRecognizer(tapGestureRecognizer)

// for webView.scrollView
let tapGestureRecognizer1 = UITapGestureRecognizer(target:self, action:#selector(self.goToback))
tapGestureRecognizer1.delegate? = self
tapGestureRecognizer1.numberOfTapsRequired = 2
 webView.scrollView.addGestureRecognizer(tapGestureRecognizer1)

If you you assign same gesture recognizer to two object then one of the object doesn't have it...see log

enter image description here

Mahendra
  • 8,448
  • 3
  • 33
  • 56
0

I have a double tap gesture working in my app by doing the following:

I created a subclass of UIWebView that is the UIGestureRecognizerDelegate

class MyWebView: UIWebView, UIGestureRecognizerDelegate {
    func gestureRecognizer(_: UIGestureRecognizer,  shouldRecognizeSimultaneouslyWith:UIGestureRecognizer) -> Bool {
        return true
    }

    func gestureRecognizer(_: UIGestureRecognizer, shouldReceive:UITouch) -> Bool {
        return true
    }
}

Then in my View Controller class I reference my subclass (make sure it is referenced in Interface Builder also)

@IBOutlet weak var webView: MyWebView!

And in viewDidLoad add the recognizer

 let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:#selector(self.goToback))
tapGestureRecognizer.delegate? = self.webView //set the delegate to the custom UIWebView
tapGestureRecognizer.numberOfTapsRequired = 2
webView.isUserInteractionEnabled = true
webView.addGestureRecognizer(tapGestureRecognizer)

Hope this helps, my original code was in Objective-C code so had to do a quick translation here so hope there are no syntax issues.

RyanZ
  • 1
  • 1
  • 2
0

I have used this code and it works for me flawlessly

override func viewDidLoad() {
    super.viewDidLoad()

    let tapLockGesture = UITapGestureRecognizer(target: self, action: #selector(tapLock))    
    tapLockGesture.delegate = self
    tapLockGesture.numberOfTapsRequired = 2
    webView.addGestureRecognizer(tapLockGesture)

}

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

make sure you conform to UIGestureRecognizerDelegate and UIScrollViewDelegate.

Aryan Sharma
  • 625
  • 2
  • 9
  • 24