以编程方式对 UILabel touchUpInside 执行操作?

发布于 2024-09-09 17:58:04 字数 101 浏览 4 评论 0原文

我有一堆通过代码添加的 UILabel,如果用户的手指触摸内部(很像 IB 中的 UIButton 的 touchUpInside),我想对每个标签执行特定的操作。这样做的最佳方法是什么?

I have a bunch of UILabels that I add through code and I want to perform a specific action for each if the user's finger touches up inside (much like UIButton's touchUpInside in IB). What is the best way of doing this?

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

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

发布评论

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

评论(2

红颜悴 2024-09-16 17:58:04

最简单的方法可能是使用 UIButton 而不是 UILabel。自定义 UIButton 没有边框,看起来像普通标签,但可以为您处理事件跟踪。

否则,您必须从 UILabel 派生并实现 UIResponder 调用以进行触摸处理。

The easiest way is probably to use UIButton instead of UILabel. A custom UIButton has no border and can look like a plain label, but handles the event tracking for you.

Otherwise, you must derive from UILabel and implement the UIResponder calls for touch handling.

请你别敷衍 2024-09-16 17:58:04

您可以使用 UITapGestureRecognizer 对标签字段执行操作。

设置 UITapGestureRecognizer 对象上的点击和触摸次数。

[oneFingerTwoTaps setNumberOfTapsRequired:1];
[oneFingerTwoTaps setNumberOfTouchesRequired:1]; 

设置标签字段的用户交互,然后

labelField.userInteractionEnabled = YES;

添加目标方法

UITapGestureRecognizer object.

You can use UITapGestureRecognizer to perform action over the label Fields.

Set the number of taps and touches on the UITapGestureRecognizer object.

[oneFingerTwoTaps setNumberOfTapsRequired:1];
[oneFingerTwoTaps setNumberOfTouchesRequired:1]; 

set user interaction of label field like

labelField.userInteractionEnabled = YES;

then add target method over the

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