ios - UITapGestureRecognizer 在 UIImageView 上工作,但不在 UILabel 上工作

标签 ios swift uitableview uilabel uitapgesturerecognizer

我有一个名为 CommentsTableViewCellUITableViewCell 类,其中包括一个 UIImageView 和一个 UILabel

我正在使用的代码:

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

如您所知,我有一个函数,每当点击 UIImageViewUILabel 时,它都会显示另一个 UIViewController

令我困惑的是 tapGestureUIImageView 上正常工作,但在 UILabel 上却不正常。

有什么想法吗?

最佳答案

你需要不同的手势来控制

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)

关于ios - UITapGestureRecognizer 在 UIImageView 上工作,但不在 UILabel 上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40483099/

相关文章:

ios - 如何让Android特定代码兼容IOS?

ios - 无法在 CocoaPods 中找到规范

ios - 将 NSString 转换为 NSIndexPath

swift - 在 Swift 3 中格式化数字

ios - 自定义 UITableViewCell 中的 UILabels 未在屏幕上呈现

ios - NSCFConstantString objectAtIndex : error in grouped table

ios - 如何仅在应用程序第一次运行时在我的 iOS 应用程序中打开页面 View Controller ?

json - UITableView 错误,Swift,JSON

ios - SpriteKit swift : How to prevent a Crash when bodyA collisions with more than one bodyB at the same time?

ios - 如何在当前 View Controller 中重新加载另一个 uiviewcontroller 的 TableView