如何快速阻止 UIPageViewController 的滑动手势?
Posted
技术标签:
【中文标题】如何快速阻止 UIPageViewController 的滑动手势?【英文标题】:how to block swipe gesture of UIPageViewController in swift? 【发布时间】:2019-07-10 06:43:33 【问题描述】:如何屏蔽UIPageViewController
的滑动手势?
pageViewController.view.isUserInteractionEnabled = false
private func setupPageController()
pageViewController = UIPageViewController(transitionStyle:.pageCurl, navigationOrientation: .horizontal, options: nil)
pageViewController.delegate = self
pageViewController.dataSource = nil
pageViewController.view.isUserInteractionEnabled = false
viewControllers = [
storyboard!.instantiateViewController(withIdentifier: "MyProfile"),
storyboard!.instantiateViewController(withIdentifier: "Sync"),
storyboard!.instantiateViewController(withIdentifier: "Assistance"),
storyboard!.instantiateViewController(withIdentifier: "ChangePassword"),
storyboard!.instantiateViewController(withIdentifier: "ContactUs"),
storyboard!.instantiateViewController(withIdentifier: "SpeedTest"),
storyboard!.instantiateViewController(withIdentifier: "Help")
]
pageViewController.setViewControllers([viewControllerAtIndex(0)!], direction: .forward, animated: true, completion: nil)
pageViewController.dataSource = self
addChildViewController(pageViewController)
view.addSubview(pageViewController.view)
pageViewController!.view.frame = CGRect(x: 0, y:110, width: view.bounds.width, height: view.bounds.height - 70)//view.bounds
pageViewController.didMove(toParentViewController: self)
// Add the page view controller's gesture recognizers to the view controller's view so that the gestures are started more easily.
// view.gestureRecognizers = pageViewController.gestureRecognizers
【问题讨论】:
How do I Disable the swipe gesture of UIPageViewController?的可能重复 使用此解决方案但不起作用。 请分享您遇到的具体问题。根据您的问题,它似乎与另一个问题相似。 禁用数据源会导致问题我无法管理内容页面之间的导航,其中每个页面都由子视图控制器管理。 【参考方案1】:您可以使用以下扩展启用或禁用SwipeGesture
extension UIPageViewController
func enableSwipeGesture()
for view in self.view.subviews
if let subView = view as? UIScrollView
subView.isScrollEnabled = true
func disableSwipeGesture()
for view in self.view.subviews
if let subView = view as? UIScrollView
subView.isScrollEnabled = false
【讨论】:
以上是关于如何快速阻止 UIPageViewController 的滑动手势?的主要内容,如果未能解决你的问题,请参考以下文章
快速滚动 UIPageViewController 阻止 viewcontroller 更新