gpt4 book ai didi

ios - Swift - 选择时的 UITableView 单元格动画

转载 作者:行者123 更新时间:2023-12-01 23:16:05 25 4
gpt4 key购买 nike

我正在使用 didSelectRow at 方法对 tableView 单元格选择进行动画处理,该方法正在运行。我的代码如下:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

let cell = tableView.cellForRow(at: indexPath)

UIView.animate(withDuration: 0.2, animations: {

cell!.transform = CGAffineTransform(scaleX: 0.97, y: 0.97)

}, completion: { finished in

UIView.animate(withDuration: 0.2) {

cell!.transform = .identity
}

})

}

我希望能够将它放入单元格的自定义类文件中,但不知道从哪里开始。这可能吗?

谢谢

最佳答案

我认为你可以使用 func setSelected(_ :animated:)

首先,您必须创建 UITableViewCell 的子类.

假设我创建了一个类名 TempTableViewCell ,在这里面,我们确实有一个预定义的函数override func setSelected(_ selected: Bool, animated: Bool) .

这里的 Selected 是单元格是否被选中的值。因此,您可以在这个函数中使用您的代码,如下所示。

例子

    class TempTableViewCell: UITableViewCell {

override func awakeFromNib() {
super.awakeFromNib()
}

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
if selected{
UIView.animate(withDuration: 0.2, animations: {
self.transform = CGAffineTransform(scaleX: 0.97, y: 0.97)
}, completion: { finished in
UIView.animate(withDuration: 0.2) {
self.transform = .identity
}
})
}
}

}

并按照以下代码在单元格中使用它,

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: TempTableViewCell.self), for: indexPath)
return cell
}

关于ios - Swift - 选择时的 UITableView 单元格动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68833594/

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