【问题标题】:UICollectionView weird animation while performBatchUpdates执行BatchUpdates时UICollectionView奇怪的动画
【发布时间】:2020-11-09 19:44:21
【问题描述】:

当在具有contentOffset.x > 0 的水平滚动UICollectionView 上调用performBatchUpdates 时,它会执行一个相当奇怪的动画。

我使用示例单元格重新创建了错误,该单元格准确显示了我们在应用程序中遇到的错误。当performBatchUpdates 中根本没有要执行的更改时,也会发生这种情况。完成后,UICollectionView中的项目是正确的,所以只是动画问题。

是否有人已经遇到过这个问题并有解决方案?

我的ViewController.swift

class CollectionViewCell: UICollectionViewCell {
    @IBOutlet var label: UILabel!
}

class ViewController: UIViewController {
    @IBOutlet private var collectionView: UICollectionView!
    
    private var items: [Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let layout = UICollectionViewFlowLayout()
        layout.estimatedItemSize = CGSize(width: 1, height: 1)
        layout.scrollDirection = .horizontal
        
        collectionView.collectionViewLayout = layout
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.contentInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
        collectionView.showsHorizontalScrollIndicator = false
    }
}

extension ViewController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        items.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? CollectionViewCell
        
        cell?.label.text = items[indexPath.row].description
        
        return cell ?? UICollectionViewCell()
    }
}

extension ViewController: UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        items.remove(at: indexPath.row)
        
        collectionView.performBatchUpdates({
            self.collectionView.deleteItems(at: [indexPath])
        }, completion: nil)
    }
}

在此处下载示例项目: https://wetransfer.com/downloads/74454a6b9df114529ba3a26b7b9488ab20200720141429/05eb4d

【问题讨论】:

  • 我下载了你的项目并运行它,首先发生的事情是一百个严重的警告被转储到控制台中。在您解决这些问题之前,绝对没有任何意义。您的单元格估计大小和自动布局约束管理不正确。

标签: ios swift xcode uicollectionview uikit


【解决方案1】:

你必须为你的collectionView单元设置itemSize

layout.itemSize = CGSize(width: 44, height: 44)

代替(或另外):

layout.estimatedItemSize = CGSize(width: 1, height: 1)

更多关于估计和何时需要:
https://developer.apple.com/documentation/uikit/uicollectionviewflowlayout/1617709-estimateditemsize

还有 itemSize:
https://developer.apple.com/documentation/uikit/uicollectionviewflowlayout/1617711-itemsize

不客气!

【讨论】:

  • 我想使用自动调整大小而不是固定项目大小。每个项目都有不同的宽度。使用layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize 也不起作用。
  • @fgroeger 如果你按照这个答案的建议去做,问题实际上会消失吗?
  • 当我设置固定的项目大小时,它可以工作,是的!但这不是一个选择,因为每个项目都有不同的宽度。
  • @fgroeger 但问题是您错误地管理了自我调整大小的单元格。无论固定大小是否正确,如果此答案解决了问题,您需要考虑这一事实。
  • 但它不能成为一个解决方案,而不仅仅是使用自定尺寸单元格,为什么它存在呢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 2019-10-09
  • 2016-08-29
  • 1970-01-01
相关资源
最近更新 更多