UILabel 有触摸方法吗?

发布于 2024-11-15 14:33:14 字数 132 浏览 3 评论 0原文

如果有人触摸预先声明的 UILabel,我想执行一个操作,例如:

if (label is touched) {
    my actions;
}

是否有方法/方式可以做到这一点?

I'd like to do an action if someone touches a predeclared UILabel, something like:

if (label is touched) {
    my actions;
}

Is there a method/way to do that?

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

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

发布评论

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

评论(5

芯好空 2024-11-22 14:33:14

您可以使用手势识别器:

- (void)someSetupMethod {
    // ...
    label.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesture = \
    [[UITapGestureRecognizer alloc]
     initWithTarget:self action:@selector(didTapLabelWithGesture:)];
    [label addGestureRecognizer:tapGesture];
    [tapGesture release];
}

- (void)didTapLabelWithGesture:(UITapGestureRecognizer *)tapGesture {
    // ...
}

You could use a gesture recognizer:

- (void)someSetupMethod {
    // ...
    label.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesture = \
    [[UITapGestureRecognizer alloc]
     initWithTarget:self action:@selector(didTapLabelWithGesture:)];
    [label addGestureRecognizer:tapGesture];
    [tapGesture release];
}

- (void)didTapLabelWithGesture:(UITapGestureRecognizer *)tapGesture {
    // ...
}
溺孤伤于心 2024-11-22 14:33:14

默认情况下,UILabel 未配置为接受触摸输入。但是,如果您使用 UIButton 并将其设置为具有自定义外观,则可以使其看起来像(单行)标签并让它响应触摸事件。

By default, UILabel isn't configured to accept touch input. However, if you use a UIButton instead and set it to have a custom appearance, you can make it look like a (single-line) label and have it respond to touch events.

等风也等你 2024-11-22 14:33:14

您可以对其进行子类化并重写触摸方法。您可能想要覆盖 touchesEnded:withEvent:

或者只使用 UIButton。

You can subclass it and override the touch methods. You probably want to override touchesEnded:withEvent:.

Or just use a UIButton.

烂柯人 2024-11-22 14:33:14

您需要确保 userinteractionenabled 设置为 YES,然后您可以覆盖 touchesBegan:withEvent:

You need to make sure userinteractionenabled is set to YES and then you can override the touchesBegan:withEvent:

与君绝 2024-11-22 14:33:14

只需为 UILabel 类添加一个类别并向其中添加您的方法即可。

Just Add A category for UILabel Class and add your method to it.

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