Get user swiping direction in UIPageViewController

Assuming you have a local array of the ViewControllers, you can use this delegate method:

func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    guard let current = self.viewControllers?.first else {
        // TODO handle error
        return
    }

    guard let index = self.pages.firstIndex(of: current) else {
        // TODO handle error
        return
    }

    self.currentIndex = index
}

The guard statements should not fail, but just in case....


For my case, I was able to determine the swiping direction by using func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [AnyObject], transitionCompleted completed: Bool) only.

Here is how. Assume that you already have a way to reference the current index named currentIndex. In the above method, you already have the information about the previousViewControllers so that you can get the previous index named previousIndex.

Basically, here is the implementation inside the delegate method:

func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [AnyObject], transitionCompleted completed: Bool) {
  if completed {
    if previousIndex < currentIndex {   // forward

    }
    else if previousIndex > currentIndex {  // reverse

    }
  }
}

Firstly in your viewControllerForIndex method assign viewcontroller.view.tag = index so that every viewcontroller.view has an assigned value

Have a currentIndex property which is initially assigned to that index of the viewcontroller which you have instantiated inside pageviewcontroller

Now in didFinishAnimating do the following

func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        if !completed {
            return     // return if animation is not completed
        }
       let pCont = bottomDataContainer?.viewControllers?[0] as? CustomVC   //take 
       out current VC

        if currentIndex < pCont?.view.tag ?? 0 {
            topNavView.goToNextDate()
        }else{
            topNavView.goToPrevDate()
        }
        selectedIndex = pCont?.view.tag ?? 0
    }

You should use both the willTransitionToViewControllers: and the didFinishAnimating: delegate methods to work out whether the transition is forward or backward. Declare a couple of index variables at the start of your class, say currentIndex and nextIndex (both Int).

In the willTransitionToViewControllers method, set the nextIndex equal to the itemIndex of the pending view controller:

EDIT

func pageViewController(pageViewController: UIPageViewController, willTransitionToViewControllers pendingViewControllers: [AnyObject]) {
    // the page view controller is about to transition to a new page, so take note
    // of the index of the page it will display.  (We can't update our currentIndex
    // yet, because the transition might not be completed - we will check in didFinishAnimating:)
    if let itemController = pendingViewControllers[0] as? PageItemController {
        nextIndex = itemController.itemIndex
    }
}

END EDIT

You can work out at this point whether the transition will be forward or backward: if nextIndex > currentIndex, then forward; if nextIndex < currentIndex then backward. Then in didFinishAnimating, if completed is true (so it completed the transition to the next view controller), set currentIndex equal to nextIndex so you can use currentIndex wherever you need to indicate which page is currently on screen:

EDIT

func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [AnyObject], transitionCompleted completed: Bool) {
    if (completed) {
        // the page view controller has successfully transitioned to the next view
        // controller, so we can update our currentIndex to the value we obtained 
        // in the willTransitionToViewControllers method:
        currentIndex = nextIndex

    }
}

Note that the first argument to these methods is the pageViewController (instance of UIPageViewController), not pageItemController which you have in your current code.

Finally, just to note: that enum you refer to (UIPageViewControllerNavigationDirection) is used only in the setViewControllers(_, direction:) method.

END EDIT