问题描述
我有一个包含 WKWebView
的视图。我的html内容是由我的应用程序生成的,所以我确定它永远不会比屏幕宽,所以我在其superview上使用 UIGestureRecognizer
来对左边和正确的刷卡。这很好。
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 UIGestureRecognizer
s on its superview to react to left and right swipes. That works great.
我还希望看到链接上没有的任何点击,这样我就可以决定我可能想要做些什么。似乎 WKWebView
吸收所有水龙头。我试着询问它的手势识别器数组,以便我可以与它们建立一些合作,但无论我什么时候问(即创建 WKWebView
之后,我都会得到nil,用我的HTML文件加载后,并在通知它已完成加载时)。这是有道理的,如果 WKWebView
没有使用 UIGestureRecognizer
机制,而只是看着点击而不是传递它们如果事实证明它对它们不感兴趣。
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.
任何获取 WKWebView
的不必要的水龙头的想法?
Any ideas for getting access to WKWebView
's unwanted taps?
推荐答案
原来解决这个问题的方法就是解决问题。我在 WKWebView
上放了一个透明的 UIView
,并附上了几个 UIGestureRecognizer
s到我的透明视图。我使用JavaScript调用来响应平移手势。当我在透明视图上识别出点击时,我查询DOM以查看它是否是链接,如果没有,我可以随心所欲地使用它。另一个好处是我可以处理长按,这是我想要做的事情,而不是让WKWebView做其烘焙,不可改变的行为。这非常有效。
Turns out the solution to this is to turn the problem inside out. I put a transparent UIView
over the WKWebView
and attached several UIGestureRecognizer
s to my transparent view. I use JavaScript calls to react to pan gestures. When a tap is recognized on my transparent view, I query the DOM to see if it is a link and if not, I can do whatever I want with it. Another benefit is that I get to handle long press, which is something I wanted to do instead of letting WKWebView do its baked-in, unchangeable behavior. This works really well.
这篇关于如何捕获WKWebView忽略的抽头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!