如何在WkWebView中检测向后滑动

时间:2018-09-06 15:10:32

标签: ios swift swipe wkwebview

当没有任何前向导航时,是否可以在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)
    }
}

1 个答案:

答案 0 :(得分:2)

我将实现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,但我希望您也需要其他人。