How to auto scroll UICollectionview in one direction swift 4?

How to auto scroll UICollectionview in one direction swift 4?



good noon



I need to scroll uicollection view cell in one direction.



For Example:-



I have array with 4 data value -> ["apple","pen","book","bottle"].



I need to scroll Uicollectionview cell like this:



apple -> pen -> book -> bottle -> apple -> pen -> book -> bottle -> continue..



i used this code for auto scroll but in my case collection view scroll like:



apple -> pen -> book -> bottle <- (scroll back to first index) apple -> pen -> book -> bottle



code:


@objc func scrollAutomatically(_ timer1: Timer)

if let coll = CV_Slider
for cell in coll.visibleCells
let indexPath: IndexPath? = coll.indexPath(for: cell)
if ((indexPath?.row)! < self.arr_img_Slide.count - 1)
let indexPath1: IndexPath?
indexPath1 = IndexPath.init(row: (indexPath?.row)! + 1, section: (indexPath?.section)!)

coll.scrollToItem(at: indexPath1!, at: .centeredHorizontally, animated: true)

else
let indexPath1: IndexPath?
indexPath1 = IndexPath.init(row: 0, section: (indexPath?.section)!)
coll.scrollToItem(at: indexPath1!, at: .centeredHorizontally, animated: true)







Thanks in advance.






i think you need circular collection view here is the link raywenderlich.com/…

– chirag shah
Sep 11 '18 at 6:59




2 Answers
2



1) return some very large number from ‘collectionView:numberOfItemsInSection:’ , like NSIntegerMax



2) in ‘collectionView:cellForItemAtIndexPath’ rather than just indexing directly into the datasource array to get the data to populate the cell use a % operator. I.E. do:


let item = datasource[indexPath.row % dataSource.count]



Instead of:


let item = datasource[indexPath.row]



This will give you an infinite and circular collectionView



If you wants to scroll only on right side, then you can do something like this:



1) add the first object to the last


["apple","pen","book","bottle","apple"]



2) Now, as you have implemented the collectionView related delegate, dataSource methods so, there are no changes in that.



3) Now as this is scrolling automatically, when the last index reaches, you should scroll to first index without animation.


@objc func scrollAutomatically(_ timer1: Timer)

if let coll = CV_Slider
for cell in coll.visibleCells
let indexPath: IndexPath? = coll.indexPath(for: cell)

//0,1,2,3,4
if ((indexPath?.row)! < self.arr_img_Slide.count - 1)
let indexPath1: IndexPath?
indexPath1 = IndexPath.init(row: (indexPath?.row)! + 1, section: (indexPath?.section)!)

coll.scrollToItem(at: indexPath1!, at: .centeredHorizontally, animated: true)
if indexPath1.row == self.arr_img_Slide.count - 1
let indexPath2: IndexPath?
indexPath2 = IndexPath.init(row: 0, section: (indexPath?.section)!)
coll.scrollToItem(at: indexPath2!, at: .centeredHorizontally, animated: false)







Try and share your results.






Have you tried the above idea?

– Bhavin Kansagara
Sep 11 '18 at 11:52






I tried this. but it not proper

– Dhaval Umraliya
Sep 21 '18 at 7:33







okay, so, what is not proper? can you add more details with the GIF or Video for the results you are getting to help you better on this.

– Bhavin Kansagara
Sep 21 '18 at 8:38



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)