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

时间:2018-05-16 00:07:47

标签: ios swift uitableview uiviewanimation

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

有人能看出这有什么问题吗?

let allCells: ((UITableViewCell) -> Void) -> Void = { handler in
    if let paths = self.savedTempTable.indexPathsForVisibleRows {
        paths.compactMap { self.savedTempTable.cellForRow(at: $0) }.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 } } )
}
)

1 个答案:

答案 0 :(得分:2)

要为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)
  }
}

结果

enter link description here

有关详细信息,请查看我的样本

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