如果有人触摸预先声明的 UILabel
,我想执行一个动作,例如:
if (label is touched) {
my actions;
}
有没有一种方法/方式可以做到这一点?
如果有人触摸预先声明的 UILabel
,我想执行一个动作,例如:
if (label is touched) {
my actions;
}
有没有一种方法/方式可以做到这一点?
您可以使用手势识别器:
- (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: