【问题标题】:How do I animate UICollectionView insert with Swift?如何使用 Swift 为 UICollectionView 插入动画?
【发布时间】:2016-11-01 13:52:26
【问题描述】:

我已经用collectionView.reloadData() 尝试过,但并没有真正奏效。我该怎么办?

【问题讨论】:

  • 不知何故,您应该使用insertItems(at:) 方法。由于没有代码 sn-p,即这是一个通用问题,我建议检查 this article -Insert Cell 部分 - 以检查您如何使用它。
  • 您的评论应该就是答案。

标签: swift animation uicollectionview


【解决方案1】:

如果您的收藏视图只有一个部分,请尝试:

self.collectionView.performBatchUpdates({
                let indexSet = IndexSet(integersIn: 0...0)
                self.collectionView.reloadSections(indexSet)
            }, completion: nil)

见于:https://stackoverflow.com/a/42001389/4455570

【讨论】:

    【解决方案2】:

    reloadData 不会改变布局或动画。 您必须更改项目的位置,例如:

    func shuffleTwoCells(srcIndex index1: Int, destIndex index2: Int) {
        let firstIndexPath  = IndexPath(row: Int(index1), section: 0)
        let secondIndexPath = IndexPath(row: Int(index2), section: 0)
        self.collectionView?.moveItem(at: firstIndexPath, to: secondIndexPath)
    }
    

    【讨论】:

    • 但那是交换两个,而不是插入一个新的,是吗?
    猜你喜欢
    • 1970-01-01
    • 2018-07-29
    • 2018-06-03
    • 1970-01-01
    • 2014-08-15
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多