【问题标题】:Adding double tap gesture recognizer to UIImageView in an UITableViewCell Swift 4+在 UITableViewCell Swift 4+ 中向 UIImageView 添加双击手势识别器
【发布时间】:2019-06-20 23:17:00
【问题描述】:

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

所以我正在尝试向我在自定义 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 
 }
}

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

【问题讨论】:

    标签: ios swift uitableview uiimageview


    【解决方案1】:

    你可能需要

    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)  
    

    并用它设置约束

    【讨论】:

    • 我编辑了帖子以包含 translatesAutoresizing。在我的实际代码中,我确实包含了只是忘记在我的帖子中输入它,因为我正在重新输入我的代码而不是复制和粘贴。抱歉输入错误。
    • 非常感谢您的解决方案有效。我为其他遇到此问题的人编辑了帖子中的代码。
    猜你喜欢
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 2017-06-10
    • 1970-01-01
    • 2020-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多