在自动滚动 UICollectionView cellForItemAtIndexPath 不是由 contentOffset 触发 [英] In auto scrolling UICollectionView cellForItemAtIndexPath not triggered by contentOffset

查看:49
本文介绍了在自动滚动 UICollectionView cellForItemAtIndexPath 不是由 contentOffset 触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个自动滚动的 UICollectionView(每个单元格都有一个图像和一个字符串).使用 contentOffset 滚动工作正常,但由于 cellForItem 永远不会被触发,新的/不可见的单元格永远不会加载.我不想使用 scrollToItem...contentOffset 允许缓慢滚动效果.我也不能使用任何需要持续时间的东西,因为我希望它一直运行,直到用户更改视图.这是我正在使用的代码:

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() 在视图加载时调用.关于如何加载不可见单元格的任何想法?

configureAutoSrollTimer() is called when the view is loaded. Any ideas on how to get the non-visible cells to load?

推荐答案

hook collectionView 的宽度约束为 IBOutlet

Hook the width constraint of the collectionView as IBOutlet

并在 viewDidLayoutSubviews 中执行此操作

and do this in viewDidLayoutSubviews

 self.collectionViewWithCon.constant = numOfCells*cellWidth

@objc func autoScrollView() {
   self.collectionViewLeadCon.constant -= 1.0
   self.collectionViewWithCon.constant += cellWidth
   collectionView.layoutIfNeeded()
 }

这篇关于在自动滚动 UICollectionView cellForItemAtIndexPath 不是由 contentOffset 触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆