In auto scrolling UICollectionView cellForItemAtIndexPath not triggered by contentOffset

做~自己de王妃 提交于 2019-12-11 17:18:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!