【问题标题】:iOS webView swipe left or right to navigateiOS webView 左右滑动导航
【发布时间】:2013-12-10 18:06:43
【问题描述】:

我正在制作一个简单的 webView iOS 7 应用程序,我希望用户能够向左或向右滑动以在网页上前进或后退。就像您在 iOS 中的 Safari 中一样。

我认为它被称为分页——但我不知道如何使用它。我该如何实施?我是菜鸟,如果你能一步一步告诉我,我将不胜感激。

谢谢

【问题讨论】:

  • WEBVIEW HTML 或 PDF 中的内容是什么?
  • 考虑使用UINavigationController。它已经支持从左到右滑动开箱,因此您只需要添加对相反方向滑动的支持。这个答案应该让你开始:stackoverflow.com/questions/5891432/…

标签: ios uiwebview swipe-gesture


【解决方案1】:

我正在我的应用程序中开发类似功能,这是我目前为止的想法 - 我还没有完成,但这里有一个开始的方法。

首先,我假设您只为 iOS 7 开发 - 虽然完全有可能为 iOS 6 构建这个,但如果您只为 7 开发,您可以采取一些简洁的捷径。

前进/后退手势由UIScreenEdgePanGestureRecognizer 处理。您可以像这样将其添加到 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];

这将添加向后滑动手势。棘手的部分是 invisibleScrollPreventer - 这是一个不完美的 hack,但它会避免滚动你的 web 视图而不是执行返回操作。 (可能有更好的方法来处理这个问题,但这是我目前的解决方案。)

在您的 swipeBack: 方法中,您将执行以下操作:

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

您将执行相同的操作以将 goForward: 方法添加到您的 Web 视图。

真正的技巧是让您在向后或向前滑动时返回(或向前)的网页出现在屏幕上,就像在 iOS 7 中的 Safari 中一样。 '我不是 100% 确定如何做到这一点(我还没有在我的应用程序中构建该功能,但我很快就会),我猜这是一个 iOS 7 snapshotView 应用了一些变暗。

根据您选择的构建方式,您可能还想查看custom navigation transitions in iOS 7。我不知道这个问题是否需要,但可能是。

祝你好运!

【讨论】:

  • 好的,谢谢!我应该知道这一点,但是我把这些代码 sn-ps 放在哪里呢? .m 还是 .h?
  • 所有这些代码都将放在您的 .m 文件中 - 如果您对 .h 和 .m 文件感到困惑,请查看这个 Stack Overflow 问题:stackoverflow.com/questions/2619048/…
  • 委托是干什么用的?
【解决方案2】:
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()

    }


}

【讨论】:

  • NSURL(string:) 构造函数中用" 替换引号字符(这将修复代码突出显示)。
  • 欢迎来到 Stack Overflow!虽然这段代码可能会回答这个问题,但最好包含一些上下文,解释它的如何工作以及何时使用它。从长远来看,纯代码的答案没有用处。
【解决方案3】:

你可以试试这个:

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
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 2013-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多