首页 > 解决方案 > 如何在动画中更改 UITableView 标签 textColor 两次?

问题描述

我试图在表格视图中突出显示文本以表明它已被复制。我首先将其更改为不同的颜色,然后在动画代码中变回黑色。但是当我运行它时,它在第一次更改后卡住了。我使用 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 } } )
}
)

标签: iosswiftuitableviewuiviewanimation

解决方案


要制作动画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/stackoverflow/tree/animate-label-text-color


推荐阅读