移动单元格时可以为 uitableViewCell 背景颜色设置动画吗?

Can a tableViewCell’s background color be animated while the cell is moved?

我基本上使用以下代码为 tableViewCell 的移动设置动画:

tableView.performBatchUpdates({ 
  tableView.moveRow(at: fromIndexPath, to: toIndexPath)
}, completion: nil)  

我的问题是:

是否可以在移动过程中为单元格的 backgroundColor 属性 设置动画?

我试图在 performBatchUpdates 块中设置一个新的 backgroundColor,但它没有动画。
我知道我可以使用 beginUpdates()endUpdates() 包含的 performBatchUpdates 指令,但这可能等同于使用 performBatchUpdates 块。

我在 this tutorial 中找到了解决方案。可以利用核心动画的功能来完成。
tableView.performBatchUpdates 块必须包含在 CATransaction.begin()CATransaction.commit() 中,并且必须为 backgroundColor:

设置一个 CABasicAnimation
    CATransaction.begin()
    tableView.performBatchUpdates({ 
        for (from, to) in movements {
            tableView.moveRow(at: from, to: to)
            guard let cell = tableView.cellForRow(at: from) else { return }
            let backgroundColorAnimation = CABasicAnimation(keyPath: #keyPath(CALayer.backgroundColor))
            backgroundColorAnimation.fromValue = cell.backgroundColor?.cgColor
            backgroundColorAnimation.toValue = kToBackgroundcolor.cgColor
            cell.layer.backgroundColor = kToBackgroundcolor.cgColor
            cell.layer.add(backgroundColorAnimation, forKey: #keyPath(CALayer.backgroundColor))
        }
    }, completion: { (finished) in
        // some code
    }
    )
    CATransaction.commit()

在这种情况下,移动的单元格的背景颜色与移动一起动画。当然,对于其他动画属性也是可以的。