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
/**
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;
if collectionView.contentSize.width <= collectionView.contentOffset.x + cellSize.width
{
collectionView.scrollRectToVisible(CGRectMake(0, contentOffset.y, cellSize.width, cellSize.height), animated: true);
} 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("scrollToNextCell"), userInfo: nil, repeats: true);
}
You can achieve that using this AutoScrollCollectionView cocoapod to scroll automatically within specific intervals
It is as simple as subclassing your collection view from AutoScrollCollectionView and call startAutoScrolling method as shown below
self.autoScrollCollectionView.startAutoScrolling(withTimeInterval: TimeInterval(exactly: 2.0)!)
self.autoScrollCollectionView.stopAutoScrolling()
/**
Scroll to Next Cell
*/
@objc func scrollToNextCell(){
//get cell size
let cellSize = CGSize(width:self.view.frame.size.width, height:viewForCV.frame.size.height)
//get current content Offset of the Collection view
let contentOffset = cvAdvertisements.contentOffset;
if cvAdvertisements.contentSize.width <= cvAdvertisements.contentOffset.x + cellSize.width
{
cvAdvertisements.scrollRectToVisible(CGRect(x:0, y:contentOffset.y, width:cellSize.width, height:cellSize.height), animated: true);
} else {
cvAdvertisements.scrollRectToVisible(CGRect(x:contentOffset.x + cellSize.width, y:contentOffset.y, width:cellSize.width, height:cellSize.height), animated: true);
}
}
/**
Invokes Timer to start Automatic Animation with repeat enabled
*/
func startTimer() {
Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(LoginViewController.scrollToNextCell), userInfo: nil, repeats: true);
}
Here is my slightly improved solution. It stops the scroll when user manually starts to scroll and restarts it if the cell is reused again.
var x = 0
weak var timer: Timer?
var cashbackOffers = [CashbackOffer]() {
didSet {
collectionView.reloadData()
autoScroll()
}
}
fileprivate func autoScroll() {
x = 0
timer?.invalidate()
timer = Timer.scheduledTimer(withTimeInterval: 3.0, repeats: true) { [weak self] timer in
guard let `self` = self else {
timer.invalidate()
return
}
if self.cashbackOffers.count == 0 {
return
}
if self.x < self.cashbackOffers.count {
let indexPath = IndexPath(item: self.x, section: 0)
self.collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
self.x = self.x + 1
} else {
self.x = 1
self.collectionView.scrollToItem(at: IndexPath(item: 0, section: 0), at: .centeredHorizontally, animated: true)
}
}
timer?.fire()
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.isTracking {
timer?.invalidate()
}
}
override func awakeFromNib() {
super.awakeFromNib()
autoScroll()
}
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()
}
Use your pagecontrol property and scrolltoitem.
func setuptimer() {
timer = Timer.scheduledTimer(timeInterval: 3, target: self , selector:
#selector(startScrolling), userInfo: nil, repeats: true)
}
@objc func startScrolling() {
if pageControl.currentPage == pageControl.numberOfPages - 1 {
pageControl.currentPage = 0
} else {
pageControl.currentPage += 1
}
<yourCollectionView>.scrollToItem(at: IndexPath(row: pageControl.currentPage, section: 0), at: .right, animated: true)
}