我正在制作一个简单的 webView iOS 7 应用程序,我希望用户能够向左或向右滑动以在网页上前进或后退。就像您在 iOS 中的 safari 中所做的那样。
我认为它叫做分页——但我不知道如何使用它。我该如何实施?我是菜鸟,所以如果你能一步步告诉我,我将不胜感激。
谢谢
我正在我的一个应用程序中开发一个类似的功能,这是我到目前为止发现的 - 我还没有完成,但这是一个开始的方法。
首先,我假设您只针对 iOS 7 进行开发——虽然完全有可能针对 iOS 6 构建它,但如果您只针对 iOS 7 进行开发,则可以采用一些简洁的快捷方式。
前进/后退手势用
UIScreenEdgePanGestureRecognizer
处理。您可以像这样将它添加到网络视图中:
UIScreenEdgePanGestureRecognizer *bezelSwipeGestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(swipeBack:)];
bezelSwipeGestureRecognizer.edges = UIRectEdgeLeft;
bezelSwipeGestureRecognizer.delegate = self;
[self.view addGestureRecognizer:bezelSwipeGestureRecognizer];
UIView *invisibleScrollPreventer = [UIView new];
invisibleScrollPreventer.frame = CGRectMake(0, 0, 10, self.view.frame.size.height);
[self.view addSubview:invisibleScrollPreventer];
这将添加向后滑动手势。棘手的部分是 invisibleScrollPreventer - 这是一个不完美的 hack,但它会避免滚动你的 web 视图而不是执行后退操作。 (可能有更好的方法来处理这个问题,但这是我目前的解决方案。)
在您的
swipeBack:
方法中,您将执行以下操作:
-(void)swipeBack:(UIScreenEdgePanGestureRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateEnded) {
if (_webView.canGoBack) {
[_webView goBack];
}
}
}
你会做这些同样的事情来添加一个
goForward:
方法到你的网络视图。
real 技巧是让您返回(或前进)的网页在您向后或向前滑动时出现在屏幕上,就像在 iOS 7 中的 Safari 中一样。我不是 100% 确定如何这样做(我还没有在我的应用程序中构建该功能,但我很快就会构建)并且我猜它是一个 iOS 7 snapshotView 应用了一些变暗。
根据您选择的构建方式,您可能还想查看 iOS 7 中的自定义导航转换。我不知道这个问题是否需要这样做,但可能是这样。
祝你好运!
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "https://www.whatever.com")
let requestObj = NSURLRequest(URL: url!)
myWebView.loadRequest(requestObj)
//swipe left or right to go back or foward
var leftSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
var rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
leftSwipe.direction = .Left
rightSwipe.direction = .Right
view.addGestureRecognizer(leftSwipe)
view.addGestureRecognizer(rightSwipe)
}
//swipe left to go forward
func handleSwipes(sender:UISwipeGestureRecognizer) {
if (sender.direction == .Left) {
myWebView.goForward()
}
//swipe right to go backward
if (sender.direction == .Right) {
myWebView.goBack()
}
}
你可以试试这个:
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://yourwebsite.com")!
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true //so easy!
}
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
webview.allowsBackForwardNavigationGestures = true