我为self.view和UILabel(mainView的子视图)添加了tapGesture,每个都执行不同的选择器。
但是唯一的主要观点是tapGesture被调用并且标签tapgesture没有被调用。如何处理?
以下是代码:
UITapGestureRecognizer *tapGesForSelf = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesForSelf:)];
[self.view addGestureRecognizer:tapGesForSelf];
UITapGestureRecognizer *tapLblClick = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesForLbl:)];
[lbl addGestureRecognizer:tapLblClick];
对于两个选择器,只有一个方法称为tapGesForSelf。 这里lbl是self.view的子视图。
答案 0 :(得分:1)
试试这个
- (void)viewDidLoad {
[super viewDidLoad];
[_lbl setUserInteractionEnabled:YES];
UITapGestureRecognizer *tapGesForSelf = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesForSelf:)];
[self.view addGestureRecognizer:tapGesForSelf];
UITapGestureRecognizer *tapLblClick = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesForLbl:)];
[_lbl addGestureRecognizer:tapLblClick];
[tapGesForSelf requireGestureRecognizerToFail:tapLblClick];
}
- (void)tapGesForSelf:(UITapGestureRecognizer *)gesture
{
NSLog(@"self");
}
- (void)tapGesForLbl:(UITapGestureRecognizer *)gesture
{
NSLog(@"label");
}
答案 1 :(得分:1)
一旦我尝试了,它就会为你的问题发布答案,并且效果很好。
首先在设计中看到标签。我将标签文本设置为“Tap Me”
现在我设置了视图和标签的代码
- (void)viewDidLoad
{
[super viewDidLoad];
//TapGesture for Label
UITapGestureRecognizer *tapLabel = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionTapLabel:)];
tapLabel.delegate = self;
tapLabel.numberOfTapsRequired = 1;
lblTapMe.userInteractionEnabled = YES;
[lblTapMe addGestureRecognizer:tapLabel];
//TapGesture for View
UITapGestureRecognizer *tapMainView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionTapMainView:)];
tapMainView.delegate = self;
tapMainView.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tapMainView];
}
//Action method for Label
-(void)actionTapLabel:(UITapGestureRecognizer *)gestureOnLabel{
UILabel *label = (UILabel *)gestureOnLabel.view;
NSLog(@"Lable text is - %@",label.text);
}
//Action method for View
-(void)actionTapMainView:(UITapGestureRecognizer *)gestureOnMainView{
NSLog(@"The Main view is tapped");
}
输出截图
答案 2 :(得分:0)
请设置UserUteractionEnabled:YES以交互TapGesture
[lbl setUserInteractionEnabled:YES];