【发布时间】:2020-01-29 08:31:25
【问题描述】:
我已经阅读了一些关于 Swift 中弹簧动画的问题并且没有正确使用,但我对这个案例有点困惑。我有一个具有 UITableView 的 ViewController。我想在它的单元格中添加一些小的弹簧弹跳动画。当一个单元格被点击时,它应该会展开并运行弹跳动画,并且它第一次可以完美运行。但是在单元格被展开并再次点击后,动画被忽略了,但animations 中的代码正在完美运行(例如打印命令)。您是否有任何想法来实现使动画工作两次或更多的目标?我想我理论上错过了一些东西。
我的视图控制器:
class TestTableViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var selectedIndex: IndexPath = IndexPath(row: 0, section: 0)
var isExpanded = [Bool]()
var currentExpandedIndexPath: IndexPath?
override func viewDidLoad() {
super.viewDidLoad()
isExpanded = Array(repeating: false, count: 15)
tableView.delegate = self
tableView.dataSource = self
}
}
extension TestTableViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 15
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TestTableViewCell", for: indexPath) as! TestTableViewCell
cell.selectionStyle = .none
cell.animate(duration: 0.5, delay: 0.2, damping: 0.5, options: .curveEaseOut)
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if isExpanded[indexPath.row] == true { return 300 }
return 150
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// This is just for handling that to be only one cell that is expanded at one time
isExpanded[indexPath.row] = !isExpanded[indexPath.row]
if (currentExpandedIndexPath != nil) {
if (indexPath == currentExpandedIndexPath) {
currentExpandedIndexPath = nil
} else {
isExpanded[currentExpandedIndexPath!.row] = false
currentExpandedIndexPath = indexPath
}
} else {
currentExpandedIndexPath = indexPath
}
tableView.beginUpdates()
tableView.reloadRows(at: [indexPath], with: .automatic)
tableView.endUpdates()
}
}
这是我的 TableViewCell 类:
class TestTableViewCell: UITableViewCell {
func animate(duration: TimeInterval, delay: TimeInterval, damping: CGFloat, options: UIView.AnimationOptions = []) {
UIView.animate(withDuration: duration, delay: delay, usingSpringWithDamping: damping, initialSpringVelocity: 1, options: options, animations: {
self.contentView.layoutIfNeeded()
print("this command runs everytime")
})
}
}
这些链接是 GIF,展示了它现在的工作方式。如果我在一个展开后点击另一个单元格,它有正确的动画(第一个链接)。但是如果我点击展开的那个,它就没有动画(第二个链接)。
【问题讨论】:
标签: ios swift uitableview ios-animations