How to capture taps ignored by WKWebView

时间:2015-11-18 21:02:12

标签: ios uigesturerecognizer wkwebview

I have a view that contains a WKWebView. My html content is generated by my app, so I know for sure it is never wider than the screen, so I'm using UIGestureRecognizers on its superview to react to left and right swipes. That works great.

I would also like to see any tap that is not on a link so I can decide what I might want to do with those. It appears that WKWebView sucks up all taps. I tried asking it for its array of gesture recognizers so that I could set up some cooperation with them, but I get nil no matter when I ask (i.e. after creating the WKWebView, after loading it with my HTML file, and when notified that it has finished loading). This makes sense if WKWebView isn't using the UIGestureRecognizer mechanism but instead is just watching for taps and not passing them along if it turns out it is not interested in them.

Any ideas for getting access to WKWebView's unwanted taps?

2 个答案:

答案 0 :(得分:7)

Swift 3.0工作解决方案,应该以相同的方式使用Obj-C(尚未测试)

...

  3
Fe       0.000000000     0.000000000     0.000000000
C        2.112450000     0.000000000     0.000000000
C        0.039817193     1.817419422     0.000000000

...

let gesture = UITapGestureRecognizer(target: self, 
                                     action: #selector(gotTap(gesture:))
)
gesture.delegate = self
self.webView?.scrollView.addGestureRecognizer(gesture)

答案 1 :(得分:6)

原来解决这个问题的方法是将问题彻底解决。我在UIView上放置了一个透明的WKWebView,并将几个UIGestureRecognizer附加到我的透明视图中。我使用JavaScript调用来响应平移手势。当我在透明视图上识别出点击时,我查询DOM以查看它是否是链接,如果没有,我可以随心所欲地做任何事情。另一个好处是我可以处理长按,这是我想做的事情,而不是让WKWebView做其烘焙,不可改变的行为。这非常有效。