问题
I am attempting to create an auto scrolling UICollectionView (each cell has an image and a string). The scrolling works fine using contentOffset
but since cellForItem
is never triggered new/non-visible cells never load. I do not want to use scrollToItem
...contentOffset
allows for a slow scrolling effect. I also can't use anything that requires a duration because I want this to run until the view is changed by the user. Here is the code I'm using:
func configAutoScrollTimer() {
signInTimer = Timer.scheduledTimer(timeInterval: 0.03, target: self, selector: #selector(autoScrollView), userInfo: nil, repeats: true)
}
func deconfigAutoScrollTimer() {
signInTimer.invalidate()
scrollX = 0
}
@objc func autoScrollView() {
scrollX += 1
let offsetPoint = CGPoint(x: scrollX, y: 0)
collectionView.contentOffset = offsetPoint
collectionView.layoutIfNeeded()
}
configureAutoSrollTimer()
is called when the view is loaded. Any ideas on how to get the non-visible cells to load?
回答1:
Hook the width constraint of the collectionView as IBOutlet
and do this in viewDidLayoutSubviews
self.collectionViewWithCon.constant = numOfCells*cellWidth
Edit:
@objc func autoScrollView() {
self.collectionViewLeadCon.constant -= 1.0
self.collectionViewWithCon.constant += cellWidth
collectionView.layoutIfNeeded()
}
来源:https://stackoverflow.com/questions/48233356/in-auto-scrolling-uicollectionview-cellforitematindexpath-not-triggered-by-conte