在UIWebView中向后/向前滑动手势?

时间:2015-08-14 14:26:40

标签: ios objective-c uiwebview uiswipegesturerecognizer

我的应用程序中有一个WebView。

因为它是一个标签式应用程序,所以我无法在网站上添加返回/转发按钮。

我想通过滑动返回/前进。从左侧/边缘向右滑动返回...就像在iOS浏览器中一样。

我该怎么办?我想我应该使用“Screen Edge Pan Gesture Recognizer”,对吧?

4 个答案:

答案 0 :(得分:12)

Swift 3中接受的答案:

override func viewDidLoad() {
    super.viewDidLoad()

    let swipeLeftRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(recognizer:)))
    let swipeRightRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(recognizer:)))
    swipeLeftRecognizer.direction = .left
    swipeRightRecognizer.direction = .right

    webView.addGestureRecognizer(swipeLeftRecognizer)
    webView.addGestureRecognizer(swipeRightRecognizer)
}

@objc private func handleSwipe(recognizer: UISwipeGestureRecognizer) {
    if (recognizer.direction == .left) {
        if webView.canGoForward {
            webView.goForward()
        }
    }

    if (recognizer.direction == .right) {
        if webView.canGoBack {
            webView.goBack()
        }
    }
}

答案 1 :(得分:5)

回答Swift 3&斯威夫特4

如果有人还有问题。这对我有用:

查找“didFinish”添加/替换以下代码。

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {

    self.didFinish()

    webView.allowsBackForwardNavigationGestures = true

}

您需要的主要代码只有一行。它位于self.didFinish()之后,但仍位于{}括号内。

webView.allowsBackForwardNavigationGestures = true

答案 2 :(得分:3)

为什么不使用滑动手势识别器?

UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];

// Setting the swipe direction.
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];

// Adding the swipe gesture on WebView
[webView addGestureRecognizer:swipeLeft];
[webView addGestureRecognizer:swipeRight];

- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {

if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
    NSLog(@"Left Swipe");
}

if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
    NSLog(@"Right Swipe");   
} 

}

答案 3 :(得分:0)

Gabriel Madruga的回答对我有用。我通过以下方式进一步减少了代码行:

  1. 将WebView拖放到情节提要中。应用所需的约束。
  2. 声明WebView的IBOutlet。我将其命名为webViewBox。
  3. 导入WebKit框架。 (导入WebKit)
  4. 在viewDidLoad()中添加以下代码行

    let leftSwipe = UISwipeGestureRecognizer(target: webViewBox, action: #selector(webViewBox.goForward))
    leftSwipe.direction = .left
    webViewBox.addGestureRecognizer(leftSwipe)
    
    let rightSwipe = UISwipeGestureRecognizer(target: webViewBox, action: #selector(webViewBox.goBack))
    leftSwipe.direction = .right
    webViewBox.addGestureRecognizer(rightSwipe)
    

您的最终结果应如下所示:

    import UIKit
    import WebKit

    class ViewController: UIViewController {


@IBOutlet weak var webViewBox: WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()


    //URL request:
    let urlReq = URLRequest(url: URL(string: "https://www.google.co.in")!)
    //Load the URL:        
    webViewBox.load(urlReq)



    //To go forward:
    let leftSwipe = UISwipeGestureRecognizer(target: webViewBox, action: #selector(webViewBox.goForward))
    leftSwipe.direction = .left
    webViewBox.addGestureRecognizer(leftSwipe)

    //To go back:
    let rightSwipe = UISwipeGestureRecognizer(target: webViewBox, action: #selector(webViewBox.goBack))
    rightSwipe.direction = .right
    webViewBox.addGestureRecognizer(rightSwipe)
  }
}