TheoK TheoK - 27 days ago 12
Swift Question

UITapGestureRecognizer working on UIImageView but not on UILabel

I have a

UITableViewCell
class named
CommentsTableViewCell
which among other things includes a
UIImageView
and a
UILabel
.

Code I'm using:

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(CommentsTableViewCell.showUserViewController))
nameLabel.userInteractionEnabled = true
avatarRoundImageView.userInteractionEnabled = true
nameLabel.addGestureRecognizer(tapGesture)
avatarRoundImageView.addGestureRecognizer(tapGesture)


As you can understand I have a function which shows another
UIViewController
whenever the
UIImageView
or the
UILabel
is tapped.

What buffles me is that the
tapGesture
works correctly on the
UIImageView
but not on the
UILabel
.

Any ideas?

Answer

You need different gesture for all control

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(CommentsTableViewCell.showUserViewController))
avatarRoundImageView.userInteractionEnabled = true
avatarRoundImageView.addGestureRecognizer(tapGesture)

let tapGesture2 = UITapGestureRecognizer(target: self, action: #selector(CommentsTableViewCell.showUserViewController))
nameLabel.userInteractionEnabled = true
nameLabel.addGestureRecognizer(tapGesture2)
Comments