Search code examples
iosswiftuipageviewcontroller

Want to have that .scroll animation, even when the datasource is not set in UIPageViewController


I have got a UIPageViewController that displays five ViewControllers (contained in an array of ViewControllers). I am using gesture recognisers to move the page in backward/forward direction, instead of the viewControllerBefore/viewControllerAfter methods. It is working fine, but the pages change suddenly. I want to have the animation similar to that when I used to get with .scroll applied. I want to be able to display two ViewControllers, similar to what we experience when we slide slowly in .scroll animation. Can you suggest how to do that? Thanks.

I was facing issues with scrolling, as I mentioned in this question -> [Disable swiping in horizontal UICollectionView inside my PageViewController to swipe in UIPageViewController So, I decided to comment this line -> self.dataSource = self And I decided to provide gesture-related functionality explicitly.

@objc func handleSwipe(gesture: UISwipeGestureRecognizer) {
    print(gesture.direction)
    switch gesture.direction {
    case UISwipeGestureRecognizer.Direction.down:
        print("down swipe")
    case UISwipeGestureRecognizer.Direction.up:
        print("up swipe")
    case UISwipeGestureRecognizer.Direction.left:

        self.setViewControllers([ViewControllerArray[1]], direction: .forward, animated: false, completion: nil)

    case UISwipeGestureRecognizer.Direction.right:

        self.setViewControllers([ViewControllerArray[2]], direction: .forward, animated: false, completion: nil)

    default:
        print("other swipe")
    }
}

Solution

  • Change animated parameter to true

    @objc func handleSwipe(gesture: UISwipeGestureRecognizer) {
        print(gesture.direction)
        switch gesture.direction {
        case UISwipeGestureRecognizer.Direction.down:
            print("down swipe")
        case UISwipeGestureRecognizer.Direction.up:
            print("up swipe")
        case UISwipeGestureRecognizer.Direction.left:
    
            self.setViewControllers([ViewControllerArray[1]], direction: .forward, animated: true, completion: nil)
    
        case UISwipeGestureRecognizer.Direction.right:
    
            self.setViewControllers([ViewControllerArray[2]], direction: .forward, animated: true, completion: nil)
    
        default:
            print("other swipe")
        }
    }