我正在用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)
},完成。{完成在中
UIView.animate(withDuration: 0.2) {
cell! .transform =.identity
}
})
}
我希望能夠把這個放在單元格的自定義類檔案中,但不知道從哪里開始。這可能嗎?
謝謝你
uj5u.com熱心網友回復:
我認為你可以使用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)
},完成。{完成在中
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
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/311732.html
標籤:
