Is there a touch method for UILabel?

走远了吗. 提交于 2019-11-29 00:21:34

问题


I'd like to do an action if someone touches a predeclared UILabel, something like:

if (label is touched) {
    my actions;
}

Is there a method/way to do that?


回答1:


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 {
    // ...
}



回答2:


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.




回答3:


You can subclass it and override the touch methods. You probably want to override touchesEnded:withEvent:.

Or just use a UIButton.




回答4:


You need to make sure userinteractionenabled is set to YES and then you can override the touchesBegan:withEvent:




回答5:


Just Add A category for UILabel Class and add your method to it.



来源:https://stackoverflow.com/questions/6324724/is-there-a-touch-method-for-uilabel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!