UILabel 有触摸方法吗?
如果有人触摸预先声明的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用手势识别器:
You could use a gesture recognizer:
默认情况下,
UILabel
未配置为接受触摸输入。但是,如果您使用UIButton
并将其设置为具有自定义外观,则可以使其看起来像(单行)标签并让它响应触摸事件。By default,
UILabel
isn't configured to accept touch input. However, if you use aUIButton
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.您可以对其进行子类化并重写触摸方法。您可能想要覆盖
touchesEnded:withEvent:
。或者只使用 UIButton。
You can subclass it and override the touch methods. You probably want to override
touchesEnded:withEvent:
.Or just use a UIButton.
您需要确保 userinteractionenabled 设置为 YES,然后您可以覆盖
touchesBegan:withEvent:
You need to make sure userinteractionenabled is set to YES and then you can override the
touchesBegan:withEvent:
只需为 UILabel 类添加一个类别并向其中添加您的方法即可。
Just Add A category for UILabel Class and add your method to it.