将 UIWebView 上的 ScreenEdgePanGesture 传递给父 PageViewController

Pass ScreenEdgePanGesture on UIWebView to parent PageViewController

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

我可以在页面上添加 ScreenEdgePanGestureRecognizer 和 segue,但这会将我置于 UIPageView 之外。我希望该解决方案是可扩展的,因为它不会转到特定页面(例如 pageViewController.setViewControllers),而只是允许 UIPageViewController 转换到 next/previous 页面。

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

    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。

在了解了 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)
    }
}