在UICollectionView 的单元格上实现动画的最佳方法是覆盖其布局UICollectionViewLayout。它的 has 方法将返回您想要显示/插入/删除的单元格的布局属性。
例如:我创建了一个类KDCollectionViewFlowLayout继承UICollectionViewFlowLayout并覆盖了delete属性。
class KDCollectionViewFlowLayout: UICollectionViewFlowLayout {
override func finalLayoutAttributesForDisappearingItemAtIndexPath(itemIndexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
let attribute = super.finalLayoutAttributesForDisappearingItemAtIndexPath(itemIndexPath)
attribute?.transform = CGAffineTransformTranslate(attributes.transform, 0, ITEM_SIZE)
attribute?.alpha = 0.0
return attribute
}
}
现在您需要将此 flowLayout 的对象分配给 viewDidLoad 中的集合视图,或者您可以通过情节提要分配它。
let flowLayout = KDCollectionViewFlowLayout()
self.collectionView?.setCollectionViewLayout(flowLayout, animated: true)
现在,只要您对collectionView 执行任何删除操作,您就可以将您定义的单元格转换为finalLayoutAttributesForDisappearingItemAtIndexPath 方法。
更新
您需要使用批处理操作从集合视图中删除项目。
collectionView.performBatchUpdates({ () -> Void in
//Array of the data which you need to deleted from collection view
let indexPaths = [NSIndexPath]()
//Delete those entery from the data base.
//TODO: Delete the information from database
//Now Delete those row from collection View
collectionView.deleteItemsAtIndexPaths(indexPaths)
}, completion:nil)