Objective c UILabel有触摸法吗?
如果有人触摸了预先声明的Objective c UILabel有触摸法吗?,objective-c,ios,cocoa-touch,uilabel,touch-event,Objective C,Ios,Cocoa Touch,Uilabel,Touch Event,如果有人触摸了预先声明的ui标签,我想做一个动作,比如: if (label is touched) { my actions; } 有什么方法/方法可以做到这一点吗?默认情况下,UILabel未配置为接受触摸输入。但是,如果使用ui按钮并将其设置为具有自定义外观,则可以使其看起来像(单行)标记并使其响应触摸事件。您需要确保userinteractionenabled设置为YES,然后您可以覆盖触摸开始:withEvent:您可以使用手势识别器: - (void)someSetupMe
ui标签
,我想做一个动作,比如:
if (label is touched) {
my actions;
}
有什么方法/方法可以做到这一点吗?默认情况下,
UILabel
未配置为接受触摸输入。但是,如果使用ui按钮
并将其设置为具有自定义外观,则可以使其看起来像(单行)标记并使其响应触摸事件。您需要确保userinteractionenabled设置为YES,然后您可以覆盖触摸开始:withEvent:您可以使用手势识别器:
- (void)someSetupMethod {
// ...
label.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = \
[[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(didTapLabelWithGesture:)];
[label addGestureRecognizer:tapGesture];
[tapGesture release];
}
- (void)didTapLabelWithGesture:(UITapGestureRecognizer *)tapGesture {
// ...
}
您可以将其子类化并覆盖触摸方法。您可能希望覆盖
touchesend:withEvent:
或者只需使用UIButton。只需为UILabel类添加一个类别,并将您的方法添加到其中 这真的很容易做到!谢谢如果我有多个标签,那么如何识别哪个标签被点击???