Forked from wassim93/automatic scroll collectionview.swift
Created
September 28, 2022 10:07
-
-
Save stvalentin/9f91a00cc95c100e89e4cb864ac7be2a to your computer and use it in GitHub Desktop.
automatic scroll collectionview swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IBOutlet weak var collectionView: UICollectionView! | |
// fill your datasource array with data | |
var datasource: [] = [] | |
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 = collectionView { | |
for cell in coll.visibleCells { | |
let indexPath: IndexPath? = coll.indexPath(for: cell) | |
if ((indexPath?.row)! < datasource.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) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment