是否可以将UITapGestureRecognizer附加到UILabel子类
我试图将手势识别器附加到我自己的UILabel的子类,但它不起作用。 你能帮我理解代码中的错误吗?
@interface Card : UILabel { } - (void) addBackSideWord; @end #import "Card.h" @implementation Card - (id)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addBackSideWord)]; [tapRecognizer setNumberOfTouchesRequired:2]; [tapRecognizer setDelegate:self]; [self addGestureRecognizer:tapRecognizer]; } return self; } - (void) addBackSideWord { //do something } @end
你的代码应该可以正常工作,唯一需要解决的是默认情况下UILabel禁用了用户交互,所以手势识别器不会收到任何触摸事件。 尝试通过将此行添加到您的代码手动启用它(例如在init方法中):
self.userInteractionEnabled = YES;
是的,这是可能的,从UIView
inheritance的任何类。
不要忘记启用用户交互。
self.userInteractionEnabled = YES;
您可以使用以下代码在UILable上添加点按手势: –
步骤1:
Delegate "UIGestureRecognizerDelegate" to your viewcontroller.h for example: @interface User_mail_List : UIViewController<UIGestureRecognizerDelegate>
第2步:
//create you UILable UILabel *title_lbl= [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)]; [title_lbl setText:@"u&me"]; [title_lbl setUserInteractionEnabled:YES]; [yourView addSubview:title_lbl];
第3步:
UITapGestureRecognizer *tap= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(Prof_lbl_Pressed:)];//your action selector [tap setNumberOfTapsRequired:1]; title_lbl.userInteractionEnabled= YES; [title_lbl addGestureRecognizer:tap];
步骤4:
-(void)Prof_lbl_Pressed:(id)sender{ //write your code action }
谢谢,
- 使用正则expression式匹配emojis以及string中的文本
- 获取容器VC而不是视图
- 具有静态表的动态类型和自定尺寸单元
- UITableview中的UICollectionView – 获取tapped UICollectionView的标记
- 通过使用XCTestExpectationunit testingasynchronous函数,但它不会等待我设置的秒
- NSRunLoop cancelPerformSelectorsWithTarget不工作
- Cordova 5 build命令是删除iOS设备方向设置
- 如何通过iPhone中的graphicsAPI在Facebook上“喜欢”和“评论”?
- Facebook最近是否禁止通过uri访问公共页面供稿?