Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Swift设置UICollectionView插入动画?_Swift_Animation_Uicollectionview - Fatal编程技术网

如何使用Swift设置UICollectionView插入动画?

如何使用Swift设置UICollectionView插入动画?,swift,animation,uicollectionview,Swift,Animation,Uicollectionview,我已经用collectionView.reloadData()尝试过了,但没有真正起作用。我该怎么办?重新加载数据既不会改变布局也不会设置动画。 您必须更改项目的位置,例如: func shuffleTwoCells(srcIndex index1: Int, destIndex index2: Int) { let firstIndexPath = IndexPath(row: Int(index1), section: 0) let secondIndexPath = In

我已经用collectionView.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)
}

如果collectionview只有一个部分,请尝试:

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

如上所示:

但这是为了交换两个,而不是插入一个新的,是吗?不知何故,您应该使用方法。由于没有代码片段,即这是一个一般性问题,我建议选中-插入单元格部分-以检查如何使用它。您的注释应该是答案。