0

我正在努力解决以下问题:我有一个带有 UICollectionViewCompositionalLayout 和两个部分的 UICollectionView。第一部分已将正交滚动行为设置为 .continues 并具有水平滚动。第二部分有垂直滚动。

当我选择一个元素时,我想将第一部分的布局从水平切换到垂直,然后从 . 布局更改应使用动画效果self.collectionView.setCollectionViewLayout(layout, animated: true)动画适用于 iOS 13 但不适用于 iOS 14(在包括 iOS 14 beta 2 在内的所有版本上测试)。

在 iOS 14 中,如果布局从水平布局更改为垂直布局,第一部分的动画将不起作用。动画被破坏/不可见并且单元格不显示。布局更改完成后,第一部分似乎是空的,并且未显示单元格。当我将集合视图滚动到底部并返回顶部时,单元格再次出现。如果第一部分的布局从垂直更改为水平,则动画也在 iOS 14 上运行,但当该部分的布局从水平更改为垂直时则不行。

import UIKit

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
    
    var isSectionVerticalLayout = false
    var collectionView: UICollectionView! = nil
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: createLayoutDiffSection())
        collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "customCellId")
        collectionView.dataSource = self
        collectionView.delegate = self
        
        view.addSubview(collectionView)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 2
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCellId", for: indexPath)
        cell.backgroundColor = UIColor(red: .random(in: 0...1),
                                       green: .random(in: 0...1),
                                       blue: .random(in: 0...1),
                                       alpha: 1.0)
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        isSectionVerticalLayout = !isSectionVerticalLayout
        let layout = createLayoutDiffSection()

        self.collectionView.setCollectionViewLayout(layout, animated: true) { (finished) in
        }
    }
    
    func createLayout(isVerticalLayout: Bool) -> NSCollectionLayoutSection {
        
        let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
                                              heightDimension: .fractionalHeight(1.0))
        let item = NSCollectionLayoutItem(layoutSize: itemSize)
        item.contentInsets = NSDirectionalEdgeInsets(top: 2, leading: 2, bottom: 2, trailing: 2)
        
        let groupHeight = NSCollectionLayoutDimension.fractionalWidth(0.5)
        let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
                                               heightDimension: groupHeight)
        let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitem: item, count: 2)
        
        let section = NSCollectionLayoutSection(group: group)
        section.contentInsets = NSDirectionalEdgeInsets(top: 20, leading: 20, bottom: 20, trailing: 20)
        
        if isVerticalLayout == false {
            section.orthogonalScrollingBehavior = .continuous
        }
        return section
    }

    
    func createLayoutDiffSection() -> UICollectionViewLayout {
        let layout = UICollectionViewCompositionalLayout { (sectionIndex: Int,
            layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? in
            
            if sectionIndex == 0 {
                return self.createLayout(isVerticalLayout: self.isSectionVerticalLayout)
            }
            else {
                return self.createLayout(isVerticalLayout: true)
            }
        }
        return layout
    }
}

知道如何解决这个问题(解决方法)或者这是 iOS 14 中的一个错误吗?

4

0 回答 0