检测 UILabel 上的触摸事件

发布于 2024-12-23 00:11:05 字数 397 浏览 1 评论 0原文

我有一个 UILabel,我正在以编程方式将其添加到我的主视图中。然后,我添加一个手势识别器来获取标签上的触摸事件:

UITapGestureRecognizer *recog = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTouchNotification)];
[notification addGestureRecognizer:recog];
[recog release];

但是,触摸标签时不会触发任何内容。我记录了所有子视图以确保标签位于顶部,确实如此。我可以让识别器工作的唯一方法是将其添加到基础视图中,但这并没有多大帮助。有谁知道为什么这个标签表现得如此“透明”?

节日快乐!

I have a UILabel that I am adding programmatically to my main view. I then add a gesture recognizer to get touch events on the label:

UITapGestureRecognizer *recog = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTouchNotification)];
[notification addGestureRecognizer:recog];
[recog release];

However, nothing fires when the label is touched. I logged all subviews to make sure the label was on top, and it is. The only way I can get the recognizer to work is if I add it to the underlying view, but that isn't much help. Does anyone know why this label is behaving so "transparently"?

Happy holidays!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往日情怀 2024-12-30 00:11:05

UILabeluserInteractionEnabled 属性默认设置为 NO,因此它不会接收触摸事件,手势识别器也不起作用。尝试为您的标签启用用户交互:

...
notification.userInteractionEnabled = YES;
...

UILabel has its userInteractionEnabled property set to NO by default so it does not receive touch events and gesture recognizer does not work. Try to enable user interaction for your label:

...
notification.userInteractionEnabled = YES;
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文