ios WKWebView不接收触摸事件

polkgigr  于 2022-11-19  发布在  iOS
关注(0)|答案(1)|浏览(306)

我正在尝试创建一个浮动的WKWebview。我希望用户可以在所有其他ViewControllers上与WKwebview交互,这就是为什么我把它放在第二个UIWindow中。问题是它不接收来自UIWindow的任何触摸事件(滚动、平移、捏合或点击,没有任何效果)。
在CustomUIWindow的init方法上,我添加了浮动的WKWebView及其ParanViewController。

init(frame: CGRect,
     statusBarStyle: UIStatusBarStyle,
     viewController:FloatableViewController) {
    
    self.floatingWindowRootViewController = viewController
    super.init(frame: frame)
    self.rootViewController = floatingWindowRootViewController
    self.addSubview(viewController.floatingView)
    self.bringSubviewToFront(viewController.floatingView)
    
}

我在网上搜索了一下,大部分的建议都是把触摸事件发送到subview。所以我在我自定义的UIWindow中覆盖了这两个函数。

override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
    
    for subView in subviews {
        if subView is WKWebView {
            return subView.hitTest(self.convert(point, to: subView), with: event) != nil
        }
    }
    return false
}

override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
    if self.point(inside: point, with: event) {
        self.pointInsideCalled = true
        return floatingWindowRootViewController?.floatingView
    }
    if pointInsideCalled {
        self.pointInsideCalled = false
        return  floatingWindowRootViewController?.floatingView
    }
    return nil
}

在我的浮动WKWebView上,我没有显式添加任何手势识别器,它被设置为isUserInteractionEnabled = true。我还可以看到我的UIWindow正在接收触摸事件。问题是我如何将触摸事件发送到我的浮动WKWebView。

hfyxw5xn

hfyxw5xn1#

我不确定这是否回答了上面的问题(这是旧的现在),也没有人试图解决我的问题....我想要一个WKWebView互动,但我也希望能够跟踪触摸。与此代码,我可以跟踪正常的开始,移动,结束触摸事件。代码使用一些自定义类,所以你不能直接复制/粘贴。我不使用故事板。在场景代理我有:

guard let windowScene = (scene as? UIWindowScene) else { return }
    let window = MyWindow(windowScene: windowScene)
    self.window = window
    window.makeKeyAndVisible()

我的窗口:

class MyWindow: UIWindow {

var webView: MWebView? = nil
var _lastValidViewIsWebView: Bool = false

override init(windowScene: UIWindowScene) {
    super.init(windowScene: windowScene)
}
required init?(coder aDecoder: NSCoder) { fatalError("") }

override func sendEvent(_ event: UIEvent) {
    super.sendEvent(event)
        
    // If webView's userInteraction ON, then MGestureView does not get events (tap, swipe)
    //      in this case, StudyView will set this class's webView, and if touch originated in this webView, forward events to MWebView manually
    
    if (webView == nil) { return }
    
    if (event.allTouches == nil) { return }
    let touches: Set<UITouch> = event.allTouches!
    if (touches.count != 1) { return }
    let touch = touches.first!
    var view = touch.view

    // view is nil for all touches for WKWebView, except for .began
    if (view == nil && touch.phase == .began) { view =  hitTest(touch.location(in: webView), with: event) }
    
    var inWebView = false
    if (view == nil) { inWebView = _lastValidViewIsWebView }    // scrolling a web view creates events that have null view.  To get around this, keep track of last valid view
    else {
        inWebView = view!.isDescendant(of: webView!)
        _lastValidViewIsWebView = inWebView;
    }
    if (!inWebView) { return }

    
    if (touch.phase == .began) { webView!.touchesBegan2(touches, with: event) }
    else if (touch.phase == .moved) { webView!.touchesMoved2(touches, with: event) }
    else if (touch.phase == .ended) {
        webView!.touchesEnded2(touches, with: event)
        _lastValidViewIsWebView = false
    }
}

}
注意:MWebView只包含一个WKWebView,外加一些逻辑。

相关问题