UILabel + touchDown

3 iphone xcode ios4

是否可以为UILabel实施触地得分?

Jef*_*ley 5

UILabel是它的子类UIView,它本身是它的子类UIResponder; 因此,绝对有可能制作一个响应触摸的标签.只需创建一个新的子类UILabel并实现以下方法:

– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:
– touchesCancelled:withEvent:
Run Code Online (Sandbox Code Playgroud)

所以,如果你想在触摸开始时发生某些事情,你就可以进行-touchesBegan:withEvent:.

如果创建一个新的子类对你来说过于苛刻,那么我建议按照@JustSid建议并使用a UIButton来完成任务.


小智 5

UILabeluserInterationEnabled默认设置为NO.

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


- (void)touchedLabel:(UIGestureRecognizer *)gesture {
NSLog(((UILabel*)gesture.view).text);
]
Run Code Online (Sandbox Code Playgroud)