Swipe gesture for back/forward in UIWebView?

2020-07-10 21:22发布

I have a WebView in my app.

Because it is a tabbed application I'm not able to add buttons for going back/forward on the website.

I want to go back/forward by swiping. Right swipe from the left side/edge is back… like in Safari browser for iOS.

How can I do it? I think i should use "Screen Edge Pan Gesture Recognizer", right?

4条回答
Animai°情兽
2楼-- · 2020-07-10 21:34

Answer in Swift 3 & Swift 4

If anyone is still having problems. This worked for me:

Find "didFinish" add / replace the following code.

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

    self.didFinish()

    webView.allowsBackForwardNavigationGestures = true

}

The main code you need is just one line. It comes after self.didFinish() but still within the {} brackets.

webView.allowsBackForwardNavigationGestures = true

查看更多
Rolldiameter
3楼-- · 2020-07-10 21:49

Accepted answer in 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()
        }
    }
}
查看更多
Bombasti
4楼-- · 2020-07-10 21:49

Gabriel Madruga's answer worked for me. I reduced the lines of code further by:

  1. Drag and drop a WebView into the storyboard. Apply required constraints.
  2. Declare the IBOutlet of the WebView. I named it webViewBox.
  3. Import WebKit framework. (import WebKit)
  4. Add the following lines of code in 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)
    

Your final result should look like this:

    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)
  }
}
查看更多
何必那么认真
5楼-- · 2020-07-10 21:53

Why not just use a swipe gesture recognizer?

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");   
} 

}
查看更多
登录 后发表回答