ios UILabel 有触摸方法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6324724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a touch method for UILabel?
提问by Matoe
I'd like to do an action if someone touches a predeclared UILabel
, something like:
如果有人触摸 predeclared UILabel
,我想采取行动,例如:
if (label is touched) {
my actions;
}
Is there a method/way to do that?
有没有办法/方法来做到这一点?
回答by Wilbur Vandrsmith
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 {
// ...
}
回答by Jonathan Grynspan
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.
默认情况下,UILabel
未配置为接受触摸输入。但是,如果您改用 aUIButton
并将其设置为具有自定义外观,则可以使其看起来像(单行)标签并使其响应触摸事件。
回答by fzwo
You can subclass it and override the touch methods. You probably want to override touchesEnded:withEvent:
.
您可以将其子类化并覆盖触摸方法。您可能想要覆盖touchesEnded:withEvent:
.
Or just use a UIButton.
或者只是使用 UIButton。
回答by Roshan Sah
Just Add A category for UILabel Class and add your method to it.
只需为 UILabel 类添加一个类别并将您的方法添加到它。
回答by Ken Pespisa
You need to make sure userinteractionenabled is set to YES and then you can override the touchesBegan:withEvent:
您需要确保 userinteractionenabled 设置为 YES,然后您可以覆盖 touchesBegan:withEvent: