简体   繁体   English

iOS webView向左或向右滑动以进行导航

[英]iOS webView swipe left or right to navigate

I'm making a simple webView iOS 7 app and I would like the user to be able to swipe left or right to go forward or backward on webpages. 我正在制作一个简单的webView iOS 7应用程序,我希望用户能够向左或向右滑动以在网页上前进或后退。 Just like you can do in safari in iOS. 就像你在iOS中的safari一样。

I think it is called pagination — but I have no idea how to use it. 我认为它被称为分页 - 但我不知道如何使用它。 How can I implement this? 我该如何实现呢? I'm a noob so if you could tell me step by step I'd appreciate it. 我是个菜鸟,所以如果你能一步一步地告诉我,我会很感激。

Thanks 谢谢

I'm working on a similar feature in an app of mine, and here's what I've figured out so far - I'm not done yet, but here's a way to get started. 我正在使用我的应用程序中的类似功能,这是我到目前为止已经想到的 - 我还没有完成,但这是一种开始的方法。

First, I'm assuming you're developing for iOS 7 only - while it's totally possible to build this for iOS 6, you can take a couple of neat shortcuts if you're only developing for 7. 首先,我假设您仅针对iOS 7进行开发 - 虽然完全有可能为iOS 6构建这个,但如果您只开发7个,则可以采用几个简洁的快捷方式。

The forward/backward gestures are handled with a UIScreenEdgePanGestureRecognizer . 使用UIScreenEdgePanGestureRecognizer处理前进/后退手势。 You add it to the web view like so: 您将其添加到Web视图,如下所示:

    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];

That'll add the back-swipe gesture. 这将添加反滑动手势。 The tricky part is that invisibleScrollPreventer - it's an imperfect hack, but it'll avoid scrolling your web view instead of doing the back action. 棘手的部分是invisibleScrollPreventer - 它是一个不完美的黑客,但它将避免滚动你的网页视图而不是做后退动作。 (There's probably a better way to handle that, but that's my current solution.) (可能有更好的方法来解决这个问题,但这是我目前的解决方案。)

In your swipeBack: method, you'll do something like the following: swipeBack:方法中,您将执行以下操作:

-(void)swipeBack:(UIScreenEdgePanGestureRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateEnded) {
        if (_webView.canGoBack) {
            [_webView goBack];
        }
    }
}

You'd do these same things to add a goForward: method to your web view. 您可以执行以下相同操作,将goForward:方法添加到Web视图中。

The real trick is getting the web page that you're going back to (or forward to) to appear onscreen as you're swiping back or forward, as in Safari in iOS 7. I'm not 100% sure how to do this (I haven't built that function yet in my app, but I will soon) and I'm guessing it's an iOS 7 snapshotView with some darkening applied. 真正的诀窍是让你回到(或转发)的网页在你向后或向前滑动时出现在屏幕上,就像iOS 7中的Safari一样。我不是百分百肯定如何做到这一点(我还没有在我的应用程序中构建该功能,但我很快就会这样做)而且我猜它是一个应用了一些变暗的iOS 7 snapshotView

Depending on how you choose to build this, you may also want to check out custom navigation transitions in iOS 7 . 根据您选择构建此项的方式,您可能还需要查看iOS 7中的自定义导航过渡 I don't know if that's required for this problem, but it might be. 我不知道这个问题是否需要,但可能是。

Good luck! 祝好运!

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

    }


}

You can try this: 你可以试试这个:

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
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM