UITapGestureRecognizer可在UIImageView上使用,但不能在UILabel上使用 [英] UITapGestureRecognizer working on UIImageView but not on UILabel

查看:56
本文介绍了UITapGestureRecognizer可在UIImageView上使用,但不能在UILabel上使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为CommentsTableViewCellUITableViewCell类,其中包括UIImageViewUILabel.

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)

正如您所了解的,每当点击UIImageViewUILabel时,我就有一个显示另一个UIViewController的函数.

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

让我感到惊讶的是,tapGestureUIImageView上正常工作,但在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上使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆