我正在使用UICollectionView,但我使用它有点像TableView。我希望通过让细胞向左侧动画并使新细胞从右侧生成动画来激活细胞。使用UITableView
,插入/移除单元格时我只能UITableViewRowAnimation.Right
动画。怎么可能用UICollectionView
来执行这样的事情?
答案 0 :(得分:2)
不幸的是,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
必须为正在添加的元素返回UICollectionViewLayoutAttributes
,其位置(attributes.center
或.frame.origin
)将从中动画到其在表格中的位置。< / p>
请注意,UICollectionViewLayoutAttributes
还有alpha
,transform
以及可用于调整动画的其他几个参数。