在 UIWebView 中滑动 back/forward 手势?
Swipe gesture for back/forward in UIWebView?
我的应用程序中有一个 WebView。
因为它是一个选项卡式应用程序,所以我无法在网站上添加用于 back/forward 的按钮。
我想back/forward滑动。从左侧向右滑动 side/edge 返回……就像在 Safari 浏览器中 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 = 真
Gabriel Madruga 的回答对我有用。我进一步减少了代码行:
- 将 WebView 拖放到故事板中。应用所需的约束。
- 声明 WebView 的 IBOutlet。我将它命名为 webViewBox.
- 导入 WebKit 框架。 (导入 WebKit)
在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)
}
}
我的应用程序中有一个 WebView。
因为它是一个选项卡式应用程序,所以我无法在网站上添加用于 back/forward 的按钮。
我想back/forward滑动。从左侧向右滑动 side/edge 返回……就像在 Safari 浏览器中 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 = 真
Gabriel Madruga 的回答对我有用。我进一步减少了代码行:
- 将 WebView 拖放到故事板中。应用所需的约束。
- 声明 WebView 的 IBOutlet。我将它命名为 webViewBox.
- 导入 WebKit 框架。 (导入 WebKit)
在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)
}
}