xcode 禁用 UIPageViewController 滑动 - Swift

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38710959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:06:02  来源:igfitidea点击:

Disable UIPageViewController Swipe - Swift

swiftxcodeuipageviewcontroller

提问by Voyager

In my project i use UIPageViewController to swipe between 5 child UIViewController. In some of child view controller, i need to disable the swipe gesture of the UIPageViewController so when user swipe it not change to other view. So how i can disable the swipe from the child view controller?

在我的项目中,我使用 UIPageViewController 在 5 个子 UIViewController 之间滑动。在某些子视图控制器中,我需要禁用 UIPageViewController 的滑动手势,以便用户滑动时不会更改为其他视图。那么如何禁用子视图控制器的滑动呢?

Appreciate for help..thanks

感谢您的帮助..谢谢

回答by Rurouni

In your page view controller, add following

在您的页面视图控制器中,添加以下内容

override func viewDidLoad(){
    super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(yourpageviewcontroller.enableSwipe(_:)), name:"enableSwipe", object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(yourpageviewcontroller.disableSwipe(_:)), name:"disableSwipe", object: nil)

}
func disableSwipe(notification: NSNotification){
    self.dataSource = nil
}

func enableSwipe(notification: NSNotification){
    self.dataSource = self
}

In your child view controller, you can just post notification by following.

在您的子视图控制器中,您可以通过以下方式发布通知。

NSNotificationCenter.defaultCenter().postNotificationName("enableSwipe", object: nil)

OR

或者

NSNotificationCenter.defaultCenter().postNotificationName("disableSwipe", object: nil)

回答by Mohsen Sedaghat Fard

You can use this extension:

你可以使用这个扩展:

extension UIPageViewController {
     var isPagingEnabled: Bool {
        get {
           var isEnabled: Bool = true
           for view in view.subviews {
               if let subView = view as? UIScrollView {
                   isEnabled = subView.isScrollEnabled
               }
           }
           return isEnabled
       }
       set {
           for view in view.subviews {
               if let subView = view as? UIScrollView {
                   subView.isScrollEnabled = newValue
               }
           }
       }
   }    
}

and call this:

并称之为:

pageCtrl.isPagingEnabled = false