Disable UIPageViewController Swipe - Swift

前端 未结 2 1008
囚心锁ツ
囚心锁ツ 2021-01-13 02:22

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 UIPageViewCo

2条回答
  •  青春惊慌失措
    2021-01-13 02:44

    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)
    

提交回复
热议问题