将UIWebView上的ScreenEdgePanGesture传递给父PageViewController(Pass ScreenEdgePanGesture on UIWebView to par

编程入门 行业动态 更新时间:2024-10-23 07:23:18
UIWebView上的ScreenEdgePanGesture传递给父PageViewController(Pass ScreenEdgePanGesture on UIWebView to parent PageViewController)

我正在嵌入Google Maps以在全宽UIWebView中导航,该UIWebView本身嵌入在UIPageViewController (在滚动模式下)。 除非 UIWebView已加载Google地图, 否则页面控制器会响应所有页面上的翻页手势,即使在UIWebView内也是如此。 显然,这是因为网页拦截了平移/轻扫手势。 我希望允许在网页内进行所有平移和交互,除非手势是边缘滑动。 如果不能进行边缘滑动,则边缘附近的平移手势也是可接受的。

我可以添加一个ScreenEdgePanGestureRecognizer和segue到一个页面,但这使我超出了UIPageView 。 我希望解决方案可以扩展,因为它不会pageViewController.setViewControllers特定页面(例如,使用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。

I am embedding Google Maps for navigation in a full-width UIWebView that is itself embedded in a UIPageViewController (in scroll mode). The page controller responds to the page turn gesture on all pages, even inside the UIWebView, unless the UIWebView has Google Maps loaded. Obviously, this is because the web page has intercepted the pan/swipe gesture. I wish to allow all panning and interaction inside the web page except if the gesture is an edge swipe. A pan gesture near the edge is also acceptable if edge swiping is not possible.

I can add a ScreenEdgePanGestureRecognizer and segue to a page but this puts me outside the UIPageView. I would like the solution to be expandable, in that it does not segue to a specific page (with pageViewController.setViewControllers for example) but just allows the UIPageViewController to transition to the next/previous page.

Here is how I am intercepting the gesture currently.

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/")!)) }

Question is: How to allow the UIPageViewController to intercept only the relevant gestures. Or to intercept the gesture then pass it to the UIPageViewController in an expandable way.

最满意答案

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

I found a way to handle this in an expandable way after learning about 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) } }

更多推荐

本文发布于:2023-08-07 00:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1457902.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ScreenEdgePanGesture   UIWebView   PageViewController   parent   Pass

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!