我的应用程序中有一个WebView。
因为它是一个标签式应用程序,所以我无法在网站上添加用于返回/转发的按钮。
我想通过滑动返回/前进。右侧滑动从左侧/边缘返回......就像在iOS浏览器中一样。
我该怎么做?我想我应该使用“Screen Edge Pan 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");
}
}
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()
}
}
}
在Swift 3和Swift 4中回答
如果有人还有问题。这对我有用:
查找“didFinish”添加/替换以下代码。
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.didFinish()
webView.allowsBackForwardNavigationGestures = true
}
您需要的主要代码只有一行。它位于self.didFinish()之后,但仍位于{}括号内。
webView.allowsBackForwardNavigationGestures = true
Gabriel Madruga的回答对我有用。我进一步减少了代码行:
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)
}
}