OStack程序员社区-中国程序员成长平台

标题: ios - 检测用户何时向左或向右滑动以在 WKWebView 上前后移动 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 19:58
标题: ios - 检测用户何时向左或向右滑动以在 WKWebView 上前后移动

我正在使用 WKWebView,我允许前后导航手势:

myWkWebView.allowsBackForwardNavigationGestures = true

现在,用户有两种返回方式:按下按钮或向左滑动。行为会有所不同,所以我想知道如何知道用户何时向左/向右滑动,以便我可以处理这个问题。

我查看了 WKNavigationDelegate 引用 (https://developer.apple.com/library/ios/documentation/WebKit/Reference/WKNavigationDelegate_Ref/),但找不到任何有用的东西。

有什么想法吗?

编辑

我忘了说我也尝试过添加滑动手势识别器,这样:

    let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(MyVC.respondToSwipeLeftOrRight(_))
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(MyVC.respondToSwipeLeftOrRight(_))
    swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
    self.view.addGestureRecognizer(swipeRight)
    self.view.addGestureRecognizer(swipeLeft)
    myWkWebView.scrollView.panGestureRecognizer.requireGestureRecognizerToFail(swipeRight)
    myWkWebView.scrollView.panGestureRecognizer.requireGestureRecognizerToFail(swipeLeft)

但是每次我向左或向右滑动时它们都不会触发。



Best Answer-推荐答案


private var currentBackForwardItem: WKBackForwardListItem?
private func handleBackForwardWebView(navigationAction: WKNavigationAction) {
    if navigationAction.navigationType == .BackForward {
        let isBack = webView.backForwardList.backList
            .filter { $0 == currentBackForwardItem }
            .count == 0

        if isBack {

        } else {

        }
    }

    currentBackForwardItem = webView.backForwardList.currentItem
}

func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
    handleBackForwardWebView(navigationAction)
    decisionHandler(.Allow)
}

关于ios - 检测用户何时向左或向右滑动以在 WKWebView 上前后移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37251895/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4