Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
613 views
in Technique[技术] by (71.8m points)

ios - How to capture taps ignored by 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Swift 3.0 working solution, should work with Obj-C the same way (haven't tested)

...

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

...

func gotTap(gesture:UITapGestureRecognizer) {
    print("gotTap")
//    self.webView?.scrollView.removeGestureRecognizer(gesture)
}

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...