title: Get user swiping direction in UIPageViewController
body: <p>These two methods <code>viewControllerBeforeViewController</code> and <code>viewControllerAfterViewController</code> of the <code>UIPageViewControllerDataSource</code> don't tell the direction of the swipe.</p>
  
  <p>The method <code>transitionCompleted</code> of the <code>UIPageViewController</code> delegate doesn't help us much either. It just tells if the page was fully swiped.</p>
  
  <p>So what method should I use to detect exactly the user direction (left or right)?</p>
  
  <p>Probably these two properties may help:</p>
  
  <pre><code>let directionForward = UIPageViewControllerNavigationDirection.Forward
  let directionReverse = UIPageViewControllerNavigationDirection.Reverse
  </code></pre>
  
  <p>My code looks like this:</p>
  
  <pre><code>import UIKit
  
  class ProView: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
  
  var pageViewController: UIPageViewController?
  
  let characterImages = ["character1", "character2", "character1", "character2", "character1", "character2", "character1", "character2"]
  
  override func viewDidLoad() {
      super.viewDidLoad()
      createPageViewController()
      setupPageControl()
  
      character = 1
  }
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
  }
  
  // Forward, check if this IS NOT the last controller
  func pageViewController(pageViewController: UIPageViewController,
      viewControllerAfterViewController ProView: UIViewController) -&gt; UIViewController? {
  
          let itemController = ProView as PageItemController
  
          // Check if there is another view
          if itemController.itemIndex+1 &lt; characterImages.count {
  
              return getItemController(itemController.itemIndex+1)
          }
  
          return nil
  }
  
  
  // Check if this IS NOT the first controller
  func pageViewController(pageViewController: UIPageViewController,
      viewControllerBeforeViewController ProView: UIViewController) -&gt; UIViewController? {
  
      let itemController = ProView as PageItemController
  
          if itemController.itemIndex &lt; 0 {
  
              return getItemController(itemController.itemIndex-1)
          }
  
          return nil
  }
  
  private func getItemController(itemIndex: Int) -&gt; PageItemController? {
  
      if itemIndex &lt; characterImages.count {
          let pageItemController = self.storyboard!.instantiateViewControllerWithIdentifier("ItemController") as PageItemController
          pageItemController.itemIndex = itemIndex
          pageItemController.imageName = characterImages[itemIndex]
          return pageItemController
  }
  
      return nil
  }
  
  func createPageViewController() {
  
      let pageController = self.storyboard!.instantiateViewControllerWithIdentifier("PageController") as UIPageViewController
      pageController.dataSource = self
      pageController.delegate = self
  
      if characterImages.count &gt; 0 {
          let firstController = getItemController(0)!
          let startingViewControllers: NSArray = [firstController]
          pageController.setViewControllers(startingViewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
      }
  
      pageViewController = pageController
      addChildViewController(pageViewController!)
      self.view.addSubview(pageViewController!.view)
      pageViewController?.didMoveToParentViewController(self)
  }
  
  func setupPageControl() {
      let appearance = UIPageControl.appearance()
      appearance.pageIndicatorTintColor = UIColor.grayColor()
      appearance.currentPageIndicatorTintColor = UIColor.whiteColor()
  }
  
  func presentationCountForPageViewController(pageViewController: UIPageViewController) -&gt; Int {
      return characterImages.count
  }
  
  func presentationIndexForPageViewController(pageViewController: UIPageViewController) -&gt; Int {
  
  
  }
  
  // BETA
  
  func pageViewController(PageItemController: UIPageViewController,
      didFinishAnimating finished: Bool,
      previousViewControllers pageViewController: [AnyObject],
      transitionCompleted completed: Bool)
  {
  
      if (!completed)
      {
          // You do nothing because whatever page you thought
          // the book was on before the gesture started is still the correct page
          return;
      }
  
      // This is where you would know the page number changed and handle it appropriately
  
      character = workaround
  
      }
  
  
  }
  
  class PageItemController: UIViewController {
  
  @IBOutlet weak var imageCharacterChoose: UIImageView!
  
  var itemIndex: Int = 0
  var imageName: String = "" {
      didSet {
  
          if let imageView = imageCharacterChoose {imageCharacterChoose.image = UIImage(named: imageName)
          }
  
      }
  }
  }
  </code></pre>
  
  <p>According to what pbasdf said,</p>
  
  <pre><code>var currentIndex: Int = 0
  var nextIndex: Int = 0
  
  func pageViewController(PageItemController: UIPageViewController,
      willTransitionToViewControllers pageViewController: [AnyObject]) {
  
          //                          pageViewController is the pending View Controller
          nextIndex = currentIndex
  
          // pageViewController...... how do I get its index?
  }
  </code></pre>