我正在尝试在UICollectionViewCell中添加自定义动画。删除动画效果很好,但是插入动画存在问题,因为视图的帧从最终值开始。
我正在尝试找到一种方法来更新视图的框架,然后触发动画。
还有其他方法可以强制视图进行布局以便生成正确的动画吗?
我已经尝试过这些方法,并且由于初始帧与to的大小不同,但是在删除时仍然可以工作,但是在插入视图时已经达到了最终的大小。如果尝试print("view.layer.animationKeys()")
,则看不到该块生成的动画。
我已经考虑过手动制作CABasicAnimations,但它似乎并没有影响子视图,因此我必须仔细研究并管理每个视图的动画。
func handle(sizeChange: (from: CGSize, to: CGSize), duration: TimeInterval) {
self.heightConstraint.constant = sizeChange.from.height
self.layoutIfNeeded()
UIView.animate(withDuration: duration, animations: {
self.heightConstraint.constant = sizeChange.to.height
self.layoutIfNeeded()
})
}