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 h
Swift 3
This tested code scrolls to the next cell and returns to first item after the last one.
func setTimer() {
let _ = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(MainVC.autoScroll), userInfo: nil, repeats: true)
}
var x = 1
@objc func autoScroll() {
if self.x < self.storiesForCollectionView.count {
let indexPath = IndexPath(item: x, section: 0)
self.collectionViewUp.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
self.x = self.x + 1
}else{
self.x = 0
self.collectionViewUp.scrollToItem(at: IndexPath(item: 0, section: 0), at: .centeredHorizontally, animated: true)
}
}
override func viewDidLoad() {
super.viewDidLoad()
setTimer()
}