是否有UILabel的触摸方法?

Mat*_*toe 19 cocoa-touch objective-c uilabel touch-event ios

如果有人触及预先声明的话UILabel,我想做一个动作,例如:

if (label is touched) {
    my actions;
}
Run Code Online (Sandbox Code Playgroud)

有方法/方法吗?

Wil*_*ith 47

您可以使用手势识别器:

- (void)someSetupMethod {
    // ...
    label.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesture = \
    [[UITapGestureRecognizer alloc]
     initWithTarget:self action:@selector(didTapLabelWithGesture:)];
    [label addGestureRecognizer:tapGesture];
    [tapGesture release];
}

- (void)didTapLabelWithGesture:(UITapGestureRecognizer *)tapGesture {
    // ...
}
Run Code Online (Sandbox Code Playgroud)


Jon*_*pan 12

默认情况下,UILabel未配置为接受触摸输入.但是,如果您使用UIButton而不是将其设置为具有自定义外观,则可以使其看起来像(单行)标签并让它响应触摸事件.