UICollectionViewController 项目插入动画 Swift

UICollectionViewController item insert animation Swift

一直在尝试为集合视图上的项目插入设置动画,动画是从屏幕外部从左到右或从右到左的幻灯片,为此我正在更改项目的框架原点 x 值。

我的问题是当我调用 UIView.animate 时立即调用完成回调而不显示动画,我尝试在项目的 class 中添加动画代码,在方法 collectionView( _:cellForItemAt :) 甚至尝试创建自定义 viewflowlayout 并覆盖 layoutAttributesForItemAtIndexPath 以添加转换,但是 none 有效,任何建议。

这个有效:

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    UIView.animate(withDuration: 1, animations: {
        var newFrame = cell.frame
        cell.frame = CGRect(x: newFrame.origin.x + 50, y: newFrame.origin.y, width: newFrame.width, height: newFrame.height)
    })
}

根据需要替换偏移量。

这也适用:

cell.frame.origin.x = cell.frame.origin.x + 50

willDisplayCell 内部