UIGestureRecognizerDelegate

UIGestureRecognizerDelegate

当没有任何向前导航时,是否可以在Web视图中检测到向后滑动?如果用户已经离开第一页,下面的代码可以正常工作,但我想知道他们是否在该页上向后滑动。
更新-这是我在修改每个注释后使用的代码。我添加了导航后退功能和刷卡手势。

    class WebViewClient: UIViewController, WKUIDelegate, WKNavigationDelegate, UIGestureRecognizerDelegate
{
    var webView: WKWebView!


    public func load(pageLink page: String)
    {
        print("Opening: " + page)

        if webView != nil
        {
            if let myURL = URL(string:page)
            {
                webUrlRequest = page

                let myRequest = URLRequest(url: myURL)

                webView.load(myRequest)
             }
      }

   func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)
    {
        print("Opened: " + webUrlRequest)
    }

    override func loadView()
    {
        let pref = WKPreferences()
        pref.javaScriptEnabled = true

        let config = WKWebViewConfiguration()
        config.preferences = pref


        webView = WKWebView(frame: .zero, configuration: config)

        webView.uiDelegate = self
        webView.navigationDelegate = self

        webView.allowsBackForwardNavigationGestures = true


        view = webView
    }

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)
    {
        if navigationAction.navigationType == .backForward && webView.backForwardList.backList.count == 0
        {
           print("Can't go back any more")
        }

        decisionHandler(.allow)
    }

    @objc func backNavigationFunction(_ sender: UIScreenEdgePanGestureRecognizer)
    {
        let dX = sender.translation(in: view).x
        if sender.state == .ended
        {
            let fraction = abs(dX / view.bounds.width)
            if fraction >= 0.35
            {
                print("Back swipe")
                //back navigation code here
            }
        }
    }

    override func viewDidLoad()
    {
        super.viewDidLoad()


        let swipeGesture = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(backNavigationFunction(_:)))
        swipeGesture.edges = .left
        swipeGesture.delegate = self

        webView.addGestureRecognizer(swipeGesture)
    }
}

最佳答案

我将实现一个UIScreenEdgePanGestureRecognizer来处理这个问题。将以下内容添加到您的viewDidLoad

let swipeGesture = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(backNavigationFunction(_:)))
    swipeGesture.edges = .left
    swipeGesture.delegate = self
    view.addGestureRecognizer(swipeGesture)

然后,实现一个函数来处理backNavigationFunction如下:
@objc func backNavigationFunction(_ sender: UIScreenEdgePanGestureRecognizer) {
    let dX = sender.translation(in: view).x
    if sender.state == .ended {
        let fraction = abs(dX / view.bounds.width)
        if fraction >= 0.35 {
            //back navigation code here
        }
    }
}

不要忘记使viewController继承自UIGestureRecognizerDelegate类(即class WebViewController: UIViewController, WKUIDelegate, WKNavigationDelegate, UIGestureRecognizerDelegate)。这里讨论的代表是UIGestureRecognizerDelegate但我预计你也需要其他人。

10-06 00:22