问题描述
我正在努力解决以下问题:我有一个带有UICollectionViewCompositionalLayout和两个部分的UICollectionView。第一部分已将troponlinescrollingBehavior设置为.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中的错误?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)