检测 UILabel 上的触摸事件
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
UILabel
的userInteractionEnabled
属性默认设置为NO
,因此它不会接收触摸事件,手势识别器也不起作用。尝试为您的标签启用用户交互:UILabel
has itsuserInteractionEnabled
property set toNO
by default so it does not receive touch events and gesture recognizer does not work. Try to enable user interaction for your label: