集合视图中的类似表视图的动画?

Tableview-like animations within a collectionview?

我使用的是 UICollectionView,但使用起来有点像 TableView。我想通过让它们向左移动并让新的单元格从右侧进入动画来为它们设置动画。使用 UITableView,我可以在插入/删除单元格时只使用 UITableViewRowAnimation.Right 动画。如何用 UICollectionView 执行这样的操作?

遗憾的是,UICollectionView 中没有此类动画的原生实现。 您可以通过创建 UICollectionViewLayout(或 UICollectionViewFlowLayout)子类并重写这两个方法来实现它们:

// This set of methods is called when the collection view undergoes an animated transition such as a batch update block or an animated bounds change.
// For each element on screen before the invalidation, finalLayoutAttributesForDisappearingXXX will be called and an animation setup from what is on screen to those final attributes.
// For each element on screen after the invalidation, initialLayoutAttributesForAppearingXXX will be called an an animation setup from those initial attributes to what ends up on screen.

func initialLayoutAttributesForAppearingItemAtIndexPath(itemIndexPath: NSIndexPath) -> UICollectionViewLayoutAttributes?

func finalLayoutAttributesForDisappearingItemAtIndexPath(itemIndexPath: NSIndexPath) -> UICollectionViewLayoutAttributes?

initialLayoutAttributesForAppearingItemAtIndexPath 必须 return UICollectionViewLayoutAttributes 对于要添加的元素,其位置(attributes.center.frame.origin)将从该位置动画到其位置table.

请注意 UICollectionViewLayoutAttributes 还有 alphatransform 和其他几个可用于调整动画的参数。