问题
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