UILabel + touchDown

Is it possible to implement touchdown for UILabel?

+3
source share
3 answers

UILabelis a subclass UIView, which in itself is a subclass UIResponder; therefore, of course, you can make a shortcut that responds to touch. Just create a new subclass UILabeland implement the following method (s):

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

So, if you want something to happen when you touch it, you do it in -touchesBegan:withEvent:.

If creating a new subclass is too hard for you, then Id will suggest doing as @JustSid suggests and use UIButtonfor the task.

+5
source

UILabel userInterationEnabled NO.

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


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

, . UIButton UIButtonTypeCustom .

-3

Source: https://habr.com/ru/post/1623273/


All Articles