UITapGestureRecognizer 无法识别水龙头
UITapGestureRecognizer not recognizing taps
我以编程方式创建了一个 viewcontroller 并在视图控制器的上半部分添加了一个子视图。但是,当我尝试向我的子视图添加点击手势时,它似乎无法识别 taps.When 我点击子视图 (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];
}
从您发送的代码中很难说出什么问题。我们不知道,menuSubView
的 class 里面是什么,如何创建 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];
}
尝试将此添加到您的代码中;
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
希望对您有所帮助。
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];
}
我以编程方式创建了一个 viewcontroller 并在视图控制器的上半部分添加了一个子视图。但是,当我尝试向我的子视图添加点击手势时,它似乎无法识别 taps.When 我点击子视图 (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];
}
从您发送的代码中很难说出什么问题。我们不知道,menuSubView
的 class 里面是什么,如何创建 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];
}
尝试将此添加到您的代码中;
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
希望对您有所帮助。
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];
}