菜鸟教程小白 发表于 2022-12-12 19:58:35

ios - 检测用户何时向左或向右滑动以在 WKWebView 上前后移动


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

<pre><code>myWkWebView.allowsBackForwardNavigationGestures = true
</code></pre>

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

<p>我查看了 WKNavigationDelegate 引用 (<a href="https://developer.apple.com/library/ios/documentation/WebKit/Reference/WKNavigationDelegate_Ref/" rel="noreferrer noopener nofollow">https://developer.apple.com/library/ios/documentation/WebKit/Reference/WKNavigationDelegate_Ref/</a>),但找不到任何有用的东西。</p>

<p>有什么想法吗?</p>

<p><strong>编辑</strong></p>

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

<pre><code>    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)
</code></pre>

<p>但是每次我向左或向右滑动时它们都不会触发。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><pre><code>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) -&gt; Void) {
    handleBackForwardWebView(navigationAction)
    decisionHandler(.Allow)
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 检测用户何时向左或向右滑动以在 WKWebView 上前后移动,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/37251895/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/37251895/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 检测用户何时向左或向右滑动以在 WKWebView 上前后移动