gpt4 book ai didi

ios - 在 UITableViewCell Swift 4+ 中向 UIImageView 添加双击手势识别器

转载 作者:可可西里 更新时间:2023-11-01 01:50:44 26 4
gpt4 key购买 nike

(使用工作解决方案编辑)

因此,我正在尝试向我在自定义 UITableViewCell 中创建的 UIImageView 添加双击手势,但似乎无法使其正常工作。

这是我的自定义 UITableViewCell:

protocol CustomCellDelegate: class {
func didTapImage()
}

class CustomCell: UITableViewCell {

//change let to lazy var
lazy var userImage: UIImageView = {
let newView = UIIMageView()
newView.layer.cornerRadius = 24
newView.layer.masksToBounds = true
newView.image = UIImage(named: "samplePic")
newView.contentMode = .scaleAspectFill
newView.isUserInteractionEnabled = true
let doubleTap = UITapGestureRecognizer(target: self, action: #selector(myFunc))
doubleTap.numberOfTouchesRequired = 1
doubleTap.numberOfTapsRequired = 2
newView.addGestureRecognizer(doubleTap)
newView.translatesAutoresizingMaskIntoConstraints = false
return newView
}

weak var delegate: CustomCellDelegate?

@objc func myFunc() {
delegate?.didTapImage()
}

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: .subTitle, reuseIdentifier: reuseIdentifier)
self.selectionStyle = .none

//changed addSubView(userImage) to self.contentView.addSubView(userImage)
self.contentView.addSubView(userImage)

NSLayoutConstraint.activate([
userImage.centerYAnchor.constraint(equalTo: self.centerYAnchor),
userImage.leftAnchor.constraint(equalTo: self.leftAnchor),
userImage.widthAnchor.constraint(equalToConstant: 48),
userImage.heightAnchor.constraint(equalToConstant: 48),
}
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

这是我的自定义 UITableViewController:

class customTableViewController: UITableViewController, CustomCellDelegate {

fileprivate let cellId = "cellId"

func didTapImage() {
print("Tapped Image")
}

override func viewDidLoad() {
super.viewDidLoad()

tableView.register(CustomCell.self, forCellReuseIdentifier: cellId)
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 72
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! CustomCell
cell.delegate = self

return cell
}
}

关于为什么这不起作用的任何想法?我究竟做错了什么?另外,如何避免在单元格出队时多次添加相同的点击手势识别器?

最佳答案

你可能需要

userImage.translatesAutoresizingMaskIntoConstraints = false

当您以编程方式创建约束时

lazy var userImage: UIImageView = {
let newView = UIIMageView()
userImage.translatesAutoresizingMaskIntoConstraints = false
newView.layer.cornerRadius = 24
newView.layer.masksToBounds = true
newView.image = UIImage(named: "samplePic")
newView.contentMode = .scaleAspectFill
newView.isUserInteractionEnabled = true
let doubleTap = UITapGestureRecognizer(target: self, action: #selector(myFunc))
doubleTap.numberOfTouchesRequired = 1
doubleTap.numberOfTapsRequired = 2
newView.addGestureRecognizer(doubleTap)
return newView
}()

还使它成为一个惰性变量而不是一个计算属性,因为每次访问都是 1 个实例,将 imageView 添加到

self.contentView.addSubView(userImage)  

并用它设置约束

关于ios - 在 UITableViewCell Swift 4+ 中向 UIImageView 添加双击手势识别器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54393767/

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