【发布时间】:2011-09-13 13:28:20
【问题描述】:
如果有人触摸了预先声明的UILabel,我想执行一个操作,例如:
if (label is touched) {
my actions;
}
有没有方法/方法可以做到这一点?
【问题讨论】:
标签: objective-c ios cocoa-touch uilabel touch-event
如果有人触摸了预先声明的UILabel,我想执行一个操作,例如:
if (label is touched) {
my actions;
}
有没有方法/方法可以做到这一点?
【问题讨论】:
标签: objective-c ios cocoa-touch uilabel touch-event
您可以使用手势识别器:
- (void)someSetupMethod {
// ...
label.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = \
[[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(didTapLabelWithGesture:)];
[label addGestureRecognizer:tapGesture];
[tapGesture release];
}
- (void)didTapLabelWithGesture:(UITapGestureRecognizer *)tapGesture {
// ...
}
【讨论】:
默认情况下,UILabel 未配置为接受触摸输入。但是,如果您改用 UIButton 并将其设置为具有自定义外观,则可以使其看起来像(单行)标签并让它响应触摸事件。
【讨论】:
您可以将其子类化并覆盖触摸方法。您可能想要覆盖 touchesEnded:withEvent:。
或者只使用 UIButton。
【讨论】:
您需要确保 userinteractionenabled 设置为 YES,然后您可以覆盖 touchesBegan:withEvent:
【讨论】:
只需为 UILabel 类添加一个类别并将您的方法添加到其中。
【讨论】: