title: CollectionView move to next cell automatically swift
body: <p>I am trying to create horizontal slider like Flipkart. I am using collectionView with Horizontal scrolling and paging. Cell contains imageView. I am succeed in scrolling items horizontally manually, but I want all these items to move automatic and manually both. I am not getting how to scroll items of collectionView automatically. Please guide me to do this. </p>
  
  <p>I have use this code in viewDidAppear:</p>
  
  <pre><code>let visibleIndexPath: NSIndexPath = self.collectionView.indexPathForCell(cell)!
  
  self.collectionView.scrollToItemAtIndexPath(visibleIndexPath,  
  atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: true)
  </code></pre>
  
  <p>Thanks.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <pre><code>import UIKit
  
  class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
  
      @IBOutlet var pageControl: UIPageControl!
      @IBOutlet var collectionView: UICollectionView!
      var begin = false
      let image1 = UIImage(named: "Slide 1")
      let image2 = UIImage(named: "Slide 2")
      let image3 = UIImage(named: "Slide 1")
  
      var images = [UIImage]()
  
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view, typically from a nib.
          images = [image1!, image2!, image3!]
          startTimer()
      }        
  
      func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
          pageControl.numberOfPages = images.count
          return images.count
      }
  
      var cell : CollectionViewCell1!
  
      func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -&gt; UICollectionViewCell {
  
        cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell1", forIndexPath: indexPath) as! CollectionViewCell1
          cell.cell_image.image = images[indexPath.row]
  
          return cell
      }
  
  
      /**
       Scroll to Next Cell
       */
      func scrollToNextCell(){
  
          //get cell size
          let cellSize = CGSizeMake(self.view.frame.width, self.view.frame.height);
  
          //get current content Offset of the Collection view
          let contentOffset = collectionView.contentOffset;
  
              //scroll to next cell
          if begin == true
          {
              pageControl.currentPage == 0
              collectionView.scrollRectToVisible(CGRectZero, animated: true)
              begin = false
          }
          else
          {
              collectionView.scrollRectToVisible(CGRectMake(contentOffset.x + cellSize.width, contentOffset.y, cellSize.width, cellSize.height), animated: true);
          }
  
      }
  
      /**
       Invokes Timer to start Automatic Animation with repeat enabled
       */
  
      func startTimer() {
  
         NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: #selector(ViewController.scrollToNextCell), userInfo: nil, repeats: true);
  
  
      }
  
      func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
  
          // If the scroll animation ended, update the page control to reflect the current page we are on
  
          pageControl.currentPage = Int((collectionView.contentOffset.x / collectionView.contentSize.width) * CGFloat(images.count))
  
          if collectionView.contentSize.width == collectionView.contentOffset.x + self.view.frame.width
          {
              begin = true
  
          }
  
      }
  
      func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView) {
  
          // Called when manually setting contentOffset
          scrollViewDidEndDecelerating(scrollView)
  
      }
  
  }
  </code></pre>
  
  <p>This is my whole code.. I am not able to go to first cell after last cell. Please Help me.</p>