UILabel + touchDown

ⅰ亾dé卋堺 提交于 2020-01-04 02:26:54

问题


Is it possible to implement touchdown for UILabel?


回答1:


UILabel is a subclass of UIView, which is itself a subclass of UIResponder; therefore, it’s definitely possible to make a label that responds to touches. Just make a new subclass of UILabel and implement the following method(s):

– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:
– touchesCancelled:withEvent:

So, if you wanted something to happen when touches started, you’d do it in -touchesBegan:withEvent:.

If creating a new subclass is too heavy-handed for you, then I’d suggest doing as @JustSid suggests and using a UIButton for the task.




回答2:


UILabel has userInterationEnabled set to NO by default.

[label setUserInteractionEnabled:YES];
[label addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(touchedLabel:)]];


- (void)touchedLabel:(UIGestureRecognizer *)gesture {
NSLog(((UILabel*)gesture.view).text);
]



回答3:


No, thats not possible. But you could use an UIButton with UIButtonTypeCustom as type for this task.



来源:https://stackoverflow.com/questions/4158099/uilabel-touchdown

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