无限滚动构图布局迅速
我正在制作一个使用构图布局的应用程序,用于CollectionView,我的Carrousel布局水平显示元素。
let itemSize = NSCollectionLayoutSize(widthDimension: .absolute(100), heightDimension: .absolute(100))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
item.contentInsets = NSDirectionalEdgeInsets(top: 2, leading: 2, bottom: 2, trailing: 2)
let groupSize = NSCollectionLayoutSize(widthDimension: .absolute(100), heightDimension: .fractionalWidth(1 / 3))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
group.contentInsets = .init(top: 4, leading: 0, bottom: 4, trailing: 0)
let section = NSCollectionLayoutSection(group: group)
section.orthogonalScrollingBehavior = .continuous
我如何才能实现无限的滚动,这意味着每次我们获得最后一个元素时,它都会回到第一个元素并继续滚动? (目前它可以正常运行,我可以滚动元素,但是每次显示最后一个元素时,我都必须以另一种方式滚动)
我已经看到了许多通过旧的CollectionView系统实现此目的的方法,但是我想继续使用新的方法
I am making an app that is using compositional layout for the collectionview I'm having a carrousel layout that display elements horizontally.
let itemSize = NSCollectionLayoutSize(widthDimension: .absolute(100), heightDimension: .absolute(100))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
item.contentInsets = NSDirectionalEdgeInsets(top: 2, leading: 2, bottom: 2, trailing: 2)
let groupSize = NSCollectionLayoutSize(widthDimension: .absolute(100), heightDimension: .fractionalWidth(1 / 3))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
group.contentInsets = .init(top: 4, leading: 0, bottom: 4, trailing: 0)
let section = NSCollectionLayoutSection(group: group)
section.orthogonalScrollingBehavior = .continuous
How can I achieve infinite scrolling meaning that every time we get the last element it goes back to the first element and continue to scroll? (for now it works, I can scroll element but every time the last element is displayed I have to scroll back the other way)
I have seen many way to achieve this with the old collectionview system, but I wanna keep using the new way please
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
维护您的数据源并返回 Int16.max 或任何大数,并执行简单的模运算,以将您的项目从数据源获取到模型中的可重用单元出列
Maintain your datasource and return Int16.max or any large number and do simple modulo to get your item from datasource to dequeue reusable cell with your model
首先,您必须向您的 imagesArray 添加 2 个附加图像,如下所示:
这样您就可以获得适当的图像数据源。
我们使用 UICollectionView 的 scrollViewDidScroll 方法来实现无限滚动。但是具有 orthogonalScrollingBehavior 的部分不会响应此方法,因此您必须使用 visibleItemsInvalidationHandler
您可以通过以下代码来实现此目的:
请注意,当您准备好显示时数据(即获得图像响应),在重新加载数据后,您必须调用:
isHorizontalScrollingEnabled(它的初始值明显等于 false)用于防止在显示数据之前进行的任何计算,并且您实际上需要它,因为在实际启动滚动之前会调用 visibleItemsInvalidationHandler 一些时间。
First, you have to add 2 more additional images to your imagesArray like this:
So you get appropriate images dataSource.
We use scrollViewDidScroll method with UICollectionView to achieve infinite scrolling. But sections with orthogonalScrollingBehavior doesn't respond to this method, so you have to use visibleItemsInvalidationHandler
You can achieve this by the following code:
Notice that when you're ready to display data(i.e. get response with images), right after reloading data you have to call:
isHorizontalScrollingEnabled(it's initial value obvious equal to false) serves to prevent any calculations before your data is displayed and you actually need it because visibleItemsInvalidationHandler is called some times before you actually initiate scrolling.