title: How do I indicate which direction the user swipes, rather the next and previous view controller in Swift?
body: <p>How do I indicate which direction the user swipes, rather the next and previous view controller?</p>
  
  <p>I have successfully set up the <code>UIPageViewControllerDataSource</code> delegate.</p>
  
  <p><img src="https://i.imgur.com/GSiWlVU.gif" alt="gif1"></p>
  
  <p>These functions do not indicate which direction the user swipes though, but rather the next and previous view controller.</p>
  
  <pre><code>func pageViewController(pageViewController: UIPageViewController,
  viewControllerBeforeViewController viewController: UIViewController) -&gt; UIViewController?
  
  func pageViewController(_ pageViewController: UIPageViewController,
  viewControllerAfterViewController viewController: UIViewController) -&gt; UIViewController?
  </code></pre>
  
  <p>I tried using this method:</p>
  
  <pre><code>func pageViewController(_ pageViewController: UIPageViewController,
            didFinishAnimating finished: Bool,
       previousViewControllers previousViewControllers: [AnyObject],
           transitionCompleted completed: Bool)
  </code></pre>
  
  <p>that comes with the <code>UIPageViewControllerDelegate</code>. <strong>I find it very difficult to integrate this method to the code</strong> I used which looks like this:</p>
  
  <pre><code>import UIKit
  
  class ProView: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
  
  var pageViewController: UIPageViewController?
  
  let characterImages = ["character1", "character2"]
  
  override func viewDidLoad() {
      super.viewDidLoad()
      createPageViewController()
      setupPageControl()
  }
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
  }
  
  func pageViewController(pageViewController: UIPageViewController,
      viewControllerAfterViewController ProView: UIViewController) -&gt; UIViewController? {
  
          let itemController = ProView as PageItemController
  
          if itemController.itemIndex+1 &lt; characterImages.count {
              return getItemController(itemController.itemIndex+1)
          }
  
          return nil
  }
  
  func pageViewController(pageViewController: UIPageViewController,
      viewControllerBeforeViewController ProView: UIViewController) -&gt; UIViewController? {
  
      let itemController = ProView as PageItemController
  
          if itemController.itemIndex &gt; 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
  
      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 {
      return 0
  }
  
  func pageViewController(pageViewController: UIPageViewController,
      didFinishAnimating finished: Bool,
      previousViewControllers ProView: [AnyObject],
      transitionCompleted completed: Bool) {
          if completed {
              println("completed!")
          } else {
              println("not completed")
          }
  }
  
  }
  
  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)
          }
  
      }
  }
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      imageCharacterChoose!.image = UIImage(named: imageName)
  
  }
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
  }
  
  }
  </code></pre>
  
  <p><strong>I need to get the <code>itemIndex</code> of the view controllers the user is browsing</strong>, but how do I do that? How do I correctly use that method? </p>
  
  <p>Thanks!</p>