CollectionView mueve a la siguiente celda automáticamente
Estoy tratando de crear un control deslizante horizontal como Flipkart. Estoy usando collectionView con desplazamiento horizontal y paginación. La celda contiene imageView. Logré desplazar los elementos horizontalmente manualmente, pero quiero que todos estos elementos se muevan tanto automática como manualmente. No entiendo cómo desplazar los elementos de collectionView automáticamente. Por favor guíame para hacer esto.
He usado este código en viewDidAppear:
let visibleIndexPath: NSIndexPath = self.collectionView.indexPathForCell(cell)!
self.collectionView.scrollToItemAtIndexPath(visibleIndexPath,
atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: true)
Gracias.
EDITAR:
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) -> Int {
pageControl.numberOfPages = images.count
return images.count
}
var cell : CollectionViewCell1!
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> 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)
}
}
Este es todo mi código. No puedo ir a la primera celda después de la última celda. Por favor, ayúdame.