本文介绍了UITapGestureRecognizer可在UIImageView上使用,但不能在UILabel上使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为CommentsTableViewCell
的UITableViewCell
类,其中包括UIImageView
和UILabel
.
I have a UITableViewCell
class named CommentsTableViewCell
which among other things includes a UIImageView
and a UILabel
.
我正在使用的代码:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(CommentsTableViewCell.showUserViewController))
nameLabel.userInteractionEnabled = true
avatarRoundImageView.userInteractionEnabled = true
nameLabel.addGestureRecognizer(tapGesture)
avatarRoundImageView.addGestureRecognizer(tapGesture)
正如您所了解的,每当点击UIImageView
或UILabel
时,我就有一个显示另一个UIViewController
的函数.
As you can understand I have a function which shows another UIViewController
whenever the UIImageView
or the UILabel
is tapped.
让我感到惊讶的是,tapGesture
在UIImageView
上正常工作,但在UILabel
上却不能正常工作.
What buffles me is that the tapGesture
works correctly on the UIImageView
but not on the UILabel
.
有什么想法吗?
推荐答案
所有控件都需要使用不同的手势
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)
这篇关于UITapGestureRecognizer可在UIImageView上使用,但不能在UILabel上使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!