我做小工具。我需要用动画改变单元格高度。点击"显示更多"去额外的控制。 enter image description here
我写了这段代码
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的天气小工具?
答案 0 :(得分:0)
我无法识别出什么是问题,因为您只发布了部分代码。但我可以建议做几个检查
此外,当动画约束更改时,您无需在动画关闭内更改它。
标准代码如
cell.favoriteConstraint.constant = newValue
UIView.animateWithDuration(0.3) {
cell.contentView.layoutIfNeeded()
}
此外,您不需要包装方法调用。这一定是好的:
showModes(alpha: 1, duration: 0.2, delay: 0.1)
tableView.beginUpdates()
tableView.endUpdates()