Removing a view controller from UIPageViewController

前端 未结 12 2517
梦谈多话
梦谈多话 2020-11-28 19:22

It\'s odd that there\'s no straightforward way to do this. Consider the following scenario:

  1. You have a page view controller with 1 page.
  2. Add another p
12条回答
  •  Happy的楠姐
    2020-11-28 19:53

    It did it the following in Swift 3.0

    fileprivate var isAnimated:Bool = false
    
    override func setViewControllers(_ viewControllers: [UIViewController]?, direction: UIPageViewControllerNavigationDirection, animated: Bool, completion: ((Bool) -> Void)? = nil) {
    
            if self.isAnimated {
                delay(0.5, closure: { 
                    self.setViewControllers(viewControllers, direction: direction, animated: animated, completion: completion)
                })
            }else {
                    super.setViewControllers(viewControllers, direction: direction, animated: animated, completion: completion)
            }
    
        }
    
    
    extension SliderViewController:UIPageViewControllerDelegate {
    
        func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
            self.isAnimated = true
        }
    
        func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
            self.isAnimated = finished
        }
    }
    

    And here the delay function

    func delay(_ delay:Double, closure:@escaping ()->()) {
        DispatchQueue.main.asyncAfter(
            deadline: DispatchTime.now() + Double(Int64(delay * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: closure)
    }
    

提交回复
热议问题