【问题标题】:Swift - UITableView Cell animation on selectionSwift - UITableView 单元格动画选择
【发布时间】:2021-10-20 07:42:17
【问题描述】:

我正在使用 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
                }
                
            })

}

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

谢谢

【问题讨论】:

    标签: ios uitableview animation transform uiviewanimation


    【解决方案1】:

    我认为你可以使用 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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-17
      • 2015-05-31
      • 2016-04-20
      • 2015-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多