ios - 如何在动画中更改 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 } } )
}
)
解决方案
要制作动画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
推荐阅读
- browser - Google oauth 首次阻止弹出窗口导致“内容安全策略指令 script-src 包含无效源:严格动态”
- javascript - 无法拉取 Github 依赖项
- ruby-on-rails - 为什么 app 初始化后 object_id 会发生变化?
- ios - 升级到 RN 0.66 和 Xcode 13 'gtest/gtest.h' 文件后无法在 Xcode 上构建 iOS 应用程序
- php - WooCommerce 来自变体属性的打开模式 - 类似于尺寸图
- apache-kafka - 使用已注册架构的 kafka-avro-console-consumer - 错误 500
- javascript - 如何成功注销并导航回 React-Native 中的主应用程序屏幕?
- javascript - 将字符串时间转换为时间戳以计算日期时差?
- java - Java,从长字符串中提取定义字符内的整数值
- spring - 终端中 SpringBoot REST API 的邮递员 POST 调用中不支持的媒体类型