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

问题描述 投票:6回答:3

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

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

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

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

ios objective-c uiwebview uiswipegesturerecognizer
3个回答
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");   
} 

}

13
投票

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()
        }
    }
}

5
投票

在Swift 3和Swift 4中回答

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

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

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

    self.didFinish()

    webView.allowsBackForwardNavigationGestures = true

}

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

webView.allowsBackForwardNavigationGestures = true


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)
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.