UITableView单元格高度动画
问题描述:
我做的部件。我需要用动画改变细胞高度。当点击“显示更多”时,去额外的控制。 enter image description hereUITableView单元格高度动画
我写了这个代码
tableView.beginUpdates()
showModes(alpha: 1, duration: 0.2, delay: 0.1)
tableView.endUpdates()
func showModes(alpha: CGFloat, duration: CGFloat, delay: CGFloat) {
if tableView.numberOfRows(inSection: 0) != 0 {
for i in 0...tableView.numberOfRows(inSection: 0)-1 {
let indexPath = IndexPath(row: i, section: 0)
if let cell = tableView.cellForRow(at: indexPath) as? WidgetCell {
UIView.animate(withDuration: TimeInterval(duration), delay: TimeInterval(delay) ,animations: {
if alpha == 0 {
cell.favoriteConstraint.constant = -45
} else {
cell.favoriteConstraint.constant = 10
}
cell.favoriteMode.alpha = alpha
self.tableView.layoutIfNeeded()
})
}
}
}
}
但它不工作的顺利开展。它会剧烈地抽动和抽动。我怎样才能让这个工作顺利进行,例如,在Apple的天气部件中?
答
因为您只发布了部分代码,所以我无法确定是什么问题。但我可以建议做几个检查
- 您是否为UITableView设置了自定义单元格? AtableView.rowHeight = UITableViewAutomaticDimension;
- 你有没有添加约束(favoriteConstraint)细胞的内容查看
此外,当动画约束改变你不需要去改变它的动画闭包内。
标准的代码看起来像
cell.favoriteConstraint.constant = newValue
UIView.animateWithDuration(0.3) {
cell.contentView.layoutIfNeeded()
}
而且你不需要来包装方法调用。这必须是确定:
showModes(alpha: 1, duration: 0.2, delay: 0.1)
tableView.beginUpdates()
tableView.endUpdates()
是的,所有加入 倍率FUNC viewDidLoad中(){ super.viewDidLoad() ledControllers = readLED() self.tableView.estimatedRowHeight = 300 self.tableView.rowHeight = UITableViewAutomaticDimension } 我录制了一段视频以使其更清晰。第一个单元展开,然后才开始动画。我希望它很像天气小工具 https://youtu.be/f4D7NnoyWrQ P.S. Судяпоникувыговоритепорусски? – MacSergey