如何在动画中更改 UITableView 标签 textColor 两次?

How can I change UITableView label textColor twice in animations?

我试图在 table 视图中突出显示文本以表明它已被复制。我先将它更改为不同的颜色,然后在动画代码中将其改回黑色。但是当我 运行 它时,它在第一次更改后就卡住了。我使用 UIColor.green 进行测试。此代码 运行 秒后,颜色保持绿色。

谁能看出这有什么问题?

let allCells: ((UITableViewCell) -> Void) -> Void = { handler in
    if let paths = self.savedTempTable.indexPathsForVisibleRows {
        paths.compactMap { self.savedTempTable.cellForRow(at: [=10=]) }.forEach { cell in
            handler(cell)
        }
        self.savedTempTable.reloadRows(at: paths, with: .none)
    }
}

UIView.animate(withDuration: 0.5,
               animations: { allCells { cell in cell.textLabel?.textColor = .green } },
               completion: { _ in
                UIView.animate(withDuration: 0.5, animations: { allCells { cell in cell.textLabel?.textColor = .black } } )
}
)

要为 textColor 设置动画,您需要改用 transition(with:duration:options:animations:completion:)

for cell in tableView.visibleCells {
  UIView.transition(with: cell, duration: 0.5, options: .transitionCrossDissolve, animations: {
    cell.textLabel?.textColor = .green
  }) { (_) in
    UIView.transition(with: cell, duration: 0.5, options: .transitionCrossDissolve, animations: {
      cell.textLabel?.textColor = .black
    }, completion: nil)
  }
}

结果

更多细节,你可以查看我的样本

https://github.com/trungducc/Whosebug/tree/animate-label-text-color