我以编程方式创建了一个viewcontroller,并在视图控制器的上半部分添加了一个子视图。然而,当我尝试向我的子视图添加点击手势时,它似乎无法识别点击。当我点击子视图(menuSubView)时,指针不会调用 - (void)tapGestureMethod。
以下是我的代码 -
UITapGestureRecognizer *tapMenuSubView=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureMethod:)];
tapMenuSubView.numberOfTapsRequired=1;
tapMenuSubView.numberOfTouchesRequired=1;
tapMenuSubView.delegate=self;
self.view.userInteractionEnabled=YES;
self.menuSubView.userInteractionEnabled=YES;
[self.menuSubView addGestureRecognizer:tapMenuSubView];
}
-(void)tapGestureMethod:(UITapGestureRecognizer *)gesture{
UIView *tapView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview: tapView];
}
答案 0 :(得分:0)
从您发送的代码中很难说,可能存在什么问题。我们不知道,menuSubView
的课程内容是什么,你如何创建menuSubView
等。
在不知情的情况下,我建议暂时删除tapMenuSubView.delegate=self;
。这取决于您的逻辑,但在基本情况下,您不需要委托来使手势识别器工作。
如果没有帮助,我会深入研究menuSubView
的设置,以确保没有任何内容拦截。也许,有一个看不见的视图覆盖它。或其他可能导致问题的手势识别器。
<强>更新强>
问题在于这种方法。
- (IBAction)menuButton:(id)sender {
MenuViewViewController *menuViewObj=[[MenuViewViewController alloc]init];
menuViewObj.view.frame=CGRectMake(0, 0, self.view.frame.size.width-80, self.view.frame.size.height);
[menuViewObj.view setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:menuViewObj.view];
}
您没有正确地将MenuViewViewController
嵌入ViewController
。 This tutorial应该有所帮助。
- (void)displayContentController:(UIViewController *) content {
[self addChildViewController:content];
content.view.frame = [self frameForContentController]; // set any frame you like
[self.view addSubview:self.currentClientView];
[content didMoveToParentViewController:self];
}
答案 1 :(得分:0)
尝试将此添加到您的代码中;
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
希望这有帮助。
答案 2 :(得分:0)
You are adding on menuSubview. Either add it on self.view or try to get it from menuSubview.
UITapGestureRecognizer *tapMenuSubView=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureMethod:)];
tapMenuSubView.numberOfTapsRequired=1;
tapMenuSubView.numberOfTouchesRequired=1;
tapMenuSubView.delegate=self;
self.view.userInteractionEnabled=YES;
[self.view addGestureRecognizer:tapMenuSubView];
}
-(void)tapGestureMethod:(UITapGestureRecognizer *)gesture{
UIView *tapView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview: tapView];
}