UICollectionViewCell 翻转动画不起作用

UICollectionViewCell flip animation does not work

我正在尝试在 UICollectionViewCell 上实现翻转动画。以下代码不起作用(根本没有动画),所以我寻求帮助。

我的自定义单元格有一个内容视图,包含两个 UIView(FrontsideView 和 BacksideView)。在它们里面我有一些带有约束的标签。

代码应在点击单元格时执行(委托工作正常)。这是我的自定义单元格:

class ProjectCVCell: UICollectionViewCell {
    
    @IBOutlet weak var frontsideView: UIView!
    @IBOutlet weak var backsideView: UIView!
    
    func flipAnimation() {
        let transitionOptions: UIView.AnimationOptions = [.transitionFlipFromRight]
        UIView.transition(with: self.contentView, duration: 0.5, options: transitionOptions, animations: {
            self.frontsideView.isHidden = !self.frontsideView.isHidden
            self.backsideView.isHidden = !self.backsideView.isHidden
        }, completion: {
            finished in
        } )
    }
}

这是 didSelectItemAt 函数:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? ProjectCVCell else { return }
        cell.flipAnimation()
}

我做错了什么?

你可以试试

UIView.transition(with: self.contentView, duration: 0.5, options: transitionOptions, animations: {
        self.frontsideView.alpha = self.frontsideView.alpha == 1.0 ? 0.0 : 1.0
        self.backsideView.alpha = self.backsideView.alpha == 1.0 ? 0.0 : 1.0
}, completion: { _ in
} )

并改变

guard let cell = collectionView.cellForItem(at:indexPath) as? ProjectCVCell else { return }