将 UIWebView 上的 ScreenEdgePanGesture 传递给父 PageViewController

Posted

技术标签:

【中文标题】将 UIWebView 上的 ScreenEdgePanGesture 传递给父 PageViewController【英文标题】:Pass ScreenEdgePanGesture on UIWebView to parent PageViewController 【发布时间】:2016-08-12 15:13:17 【问题描述】:

我在全角UIWebView 中嵌入了用于导航的谷歌地图,它本身嵌入在UIPageViewController 中(在滚动模式下)。页面控制器响应所有页面上的翻页手势,即使在 UIWebView 内,除非UIWebView 已加载 Google 地图。很明显,这是因为网页截取了平移/滑动手势。我希望允许网页内的所有平移和交互,除非手势是边缘滑动。如果边缘滑动是不可能的,靠近边缘的平移手势也是可以接受的。

我可以添加一个ScreenEdgePanGestureRecognizer 并转到一个页面,但这会让我在UIPageView 之外。我希望解决方案是可扩展的,因为它不会转到特定页面(例如 pageViewController.setViewControllers),而只是允许 UIPageViewController 转换到下一个/上一个页面。

这是我目前拦截手势的方式。

    var leftScreenPanGestureRecognizer = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(self.handleMapSwipe(_:)))
    leftScreenPanGestureRecognizer.edges = .Left
    leftScreenPanGestureRecognizer.cancelsTouchesInView = true
    self.view.addGestureRecognizer(leftScreenPanGestureRecognizer)
    self.webView.addGestureRecognizer(leftScreenPanGestureRecognizer)


    if let location = locationManager.location?.coordinate 
        let lat = location.latitude
        let long = location.longitude
        webView.loadRequest(NSURLRequest(URL: NSURL(string: "https://www.google.com/maps/place/\(lat),\(long)/")!))
     else 
        webView.loadRequest(NSURLRequest(URL: NSURL(string: "https://www.google.com/maps/")!))
    

问题是: 如何让 UIPageViewController 只拦截相关的手势。或者拦截手势,然后以可扩展的方式将其传递给 UIPageViewController。

【问题讨论】:

【参考方案1】:

在了解了self.parentViewController之后,我找到了一种可扩展的方式来处理这个问题

@IBAction func handleMapBackwardSwipe(recognizer: UIScreenEdgePanGestureRecognizer) 
    if (recognizer.state == .Began) 
        webView.scrollView.scrollEnabled = true
     else if(recognizer.state == .Ended || recognizer.state == .Cancelled)
        webView.scrollView.scrollEnabled = false
    

    // PageViewController is the controlling class for the UIPageViewController on the storyboard.
    let pvc = (self.parentViewController as! PageViewController)
    let newView = pvc.pageViewController(pvc, viewControllerBeforeViewController: self)
    if let newView = newView 
        pvc.setViewControllers([newView], direction: .Reverse, animated: true, completion: nil)
    


@IBAction func handleMapForwardSwipe(recognizer: UIScreenEdgePanGestureRecognizer) 
    if (recognizer.state == .Began) 
        webView.scrollView.scrollEnabled = true
     else if(recognizer.state == .Ended || recognizer.state == .Cancelled)
        webView.scrollView.scrollEnabled = false
    

    let pvc = (self.parentViewController as! PageViewController)
    let newView = pvc.pageViewController(pvc, viewControllerAfterViewController: self)
    if let newView = newView 
        pvc.setViewControllers([newView], direction: .Forward, animated: true, completion: nil)
    

【讨论】:

以上是关于将 UIWebView 上的 ScreenEdgePanGesture 传递给父 PageViewController的主要内容,如果未能解决你的问题,请参考以下文章

在 iPhone 上的 UIWebView 中实现翻页式动画

在 swift 4 上单击 UIWebview 上的播放视频时出错

在 iOS 中禁用 uiwebview 上的某些触摸事件

如何在 UIWebView 上的键盘上方显示工具栏 [关闭]

如何将 iPhone 联系人拉入 UIWebView?

UIWebView 在 Safari 中启动