gpt4 book ai didi

ios - Swift spring 动画在 UITableViewCell 上无法正常工作

转载 作者:行者123 更新时间:2023-11-30 10:27:04 27 4
gpt4 key购买 nike

我已经阅读了一些问题,并且没有正确使用 Swift 中的 spring 动画,但我对这种情况有点困惑。我有一个 ViewController,它有一个 UITableView。我想在其单元格中添加一些小的 Spring 弹跳动画。当一个单元格被点击时,它应该展开并运行弹跳动画,并且它第一次运行得很好。但是,在展开并再次点击单元格后,动画将被忽略,但动画内的代码可以完美运行(例如打印命令)。你有什么想法来实现这个目标,让动画工作两次或更多次吗?我想理论上我错过了一些东西。

我的 View Controller :

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,显示了它现在的工作原理。如果我在展开后点击另一个单元格,它就会有正确的动画(第一个链接)。但如果我点击展开的那个,它就不会动画(第二个链接)。

Tapping one after one expanded

Tapping the same cell after it is expanded

最佳答案

这里有两种可能性:

1 - 以下代码可能会覆盖您的动画:

tableView.reloadRows(at: [indexPath], with: .automatic)

尝试传递 .none 或其他一些标志。

2 - 必须从主线程(又名 DispatchQueue.main)调用以下代码。

self.contentView.layoutIfNeeded()

还有第三种可能性,即 reloadRows 不一定会在您当前处理动画的位置调用 cellForRowAt

关于ios - Swift spring 动画在 UITableViewCell 上无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59963156/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com