简体   繁体   中英

Adding double tap gesture recognizer to UIImageView in an UITableViewCell Swift 4+

(Edited with working solution)

So I'm trying to add a double tap gesture to an UIImageView I created in a custom UITableViewCell but can't seem to get it working.

Here is my custom 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")
}

Here is my custom 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 
 }
}

Any ideas as to why this isn't working? What am I doing wrong? Also how do I avoid having the same tap gestures recognizer added multiple times as cells are dequeue?

You may need

userImage.translatesAutoresizingMaskIntoConstraints = false

as you create constraints programmatically

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 
}()

also make it a lazy var not a computed property for being 1 instance every access , add the imageView to

self.contentView.addSubView(userImage)  

and set the constraints with it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM