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
try this
var index = 0
var inForwardDirection = true
var timer: Timer?
@objc func scrollToNextCell() {
//scroll to next cell
let items = collectionViewTable.numberOfItems(inSection: 0)
if (items - 1) == index {
collectionViewTable.scrollToItem(at: IndexPath(row: index, section: 0), at: UICollectionViewScrollPosition.right, animated: true)
} else if index == 0 {
collectionViewTable.scrollToItem(at: IndexPath(row: index, section: 0), at: UICollectionViewScrollPosition.left, animated: true)
} else {
collectionViewTable.scrollToItem(at: IndexPath(row: index, section: 0), at: UICollectionViewScrollPosition.centeredHorizontally, animated: true)
}
if inForwardDirection {
if index == (items - 1) {
index -= 1
inForwardDirection = false
} else {
index += 1
}
} else {
if index == 0 {
index += 1
inForwardDirection = true
} else {
index -= 1
}
}
}
/**
call this method when collection view loaded
*/
func startTimer() {
if timer == nil {
timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(scrollToNextCell), userInfo: nil, repeats: true);
}
}
I suggest using the collection view method scrollToItem(at:at:animated:)
rather than scrollRectToVisible. It saves you from having to do calculations to figure out what rectangle to expose. You can just specify an indexPath to which you want to scroll.
swift 4:
func scrollToNextCell(){
//get cell size
let cellSize = view.frame.size
//get current content Offset of the Collection view
let contentOffset = collectionView.contentOffset
if collectionView.contentSize.width <= collectionView.contentOffset.x + cellSize.width
{
let r = CGRect(x: 0, y: contentOffset.y, width: cellSize.width, height: cellSize.height)
collectionView.scrollRectToVisible(r, animated: true)
} else {
let r = CGRect(x: contentOffset.x + cellSize.width, y: contentOffset.y, width: cellSize.width, height: cellSize.height)
collectionView.scrollRectToVisible(r, animated: true);
}
}
Below is code you can try :
/**
Scroll to Next Cell
*/
func scrollToNextCell(){
//get Collection View Instance
let collectionView:UICollectionView;
//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
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() {
let timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("scrollToNextCell"), userInfo: nil, repeats: true);
}
For Swift4
override func viewDidLoad() {
super.viewDidLoad()
startTimer()
}
func startTimer() {
let timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.scrollAutomatically), userInfo: nil, repeats: true)
}
@objc func scrollAutomatically(_ timer1: Timer) {
if let coll = topMenuCollection {
for cell in coll.visibleCells {
let indexPath: IndexPath? = coll.indexPath(for: cell)
if ((indexPath?.row)! < banner.count - 1){
let indexPath1: IndexPath?
indexPath1 = IndexPath.init(row: (indexPath?.row)! + 1, section: (indexPath?.section)!)
coll.scrollToItem(at: indexPath1!, at: .right, animated: true)
}
else{
let indexPath1: IndexPath?
indexPath1 = IndexPath.init(row: 0, section: (indexPath?.section)!)
coll.scrollToItem(at: indexPath1!, at: .left, animated: true)
}
}
}
}
topMenuCollection :- your collection view
banner.Count:- a number of cells containing a collection view