【问题标题】:Infinite scrolling compositional layout Swift无限滚动组合布局 Swift
【发布时间】:2022-06-28 20:49:57
【问题描述】:

我正在为 collectionview 制作一个使用组合布局的应用程序我有一个水平显示元素的轮播布局。

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 系统实现这一点,但我想继续使用新的方法

【问题讨论】:

    标签: swift uikit


    【解决方案1】:

    维护您的数据源并返回 Int16.max 或任何大数,并进行简单的模运算以将您的项目从数据源中取出,以便使用您的模型将可重用单元格出列

    import UIKit
    
    class ViewController: UIViewController {
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    var sections: [Int] = Array(0...7)
    var models: [[Int]] = Array(repeating: [1,2,3,4,5,6,7,8], count: 8)
    
    var sectionCellColors: [Int: UIColor] = [
        0 : .blue,
        1 : .red,
        2 : .green,
        3 : .yellow,
        4 : .magenta,
        5 : .brown,
        6 : .gray,
        7 : .black
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.backgroundColor = .cyan
        let layout = createLayout()
        collectionView.setCollectionViewLayout(layout, animated: false)
    }
    
    func createLayout() -> UICollectionViewCompositionalLayout {
        let layout = UICollectionViewCompositionalLayout { sectionNum, environment in
            let item = NSCollectionLayoutItem(
                layoutSize: NSCollectionLayoutSize(
                    widthDimension: .fractionalWidth(0.2),
                    heightDimension: .fractionalHeight(1)))
            item.contentInsets = .init(top: 8, leading: 8, bottom: 8, trailing: 8)
            let group = NSCollectionLayoutGroup.horizontal(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .absolute(300)), subitems: [item])
            let section = NSCollectionLayoutSection(group: group)
            section.orthogonalScrollingBehavior = .continuous
            return section
        }
        return layout
    }
    
    override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
        if context.nextFocusedView?.isKind(of: UICollectionViewCell.self) == true {
            context.nextFocusedView?.layer.borderColor = UIColor.purple.cgColor
            context.nextFocusedView?.layer.borderWidth = 2
        }
        if context.previouslyFocusedView?.isKind(of: UICollectionViewCell.self) == true {
            context.nextFocusedView?.layer.borderColor = UIColor.clear.cgColor
            context.nextFocusedView?.layer.borderWidth = 0
        }
    }
    }
    
    extension ViewController: UICollectionViewDataSource {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return Int(Int16.max)
    //        return sections.count
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return models[section % sections.count].count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellID", for: indexPath)
        cell.backgroundColor = sectionCellColors[indexPath.section % sections.count]
        return cell
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-07
      • 1970-01-01
      • 1970-01-01
      • 2020-11-09
      相关资源
      最近更新 更多