将点击手势添加到 tabBarController
我有一个关于如何向 UITabBarController 添加点击手势的问题。由于 UITabBarController 已经内置了点击手势(响应标签栏上选项卡栏项目的点击),虽然从技术上讲我可以将自己的手势添加到 tabBarController,但 tabBar 失去了自己的本机点击手势。下面是我想要做的:
UIViewController *VC1 = ....;
UIViewController *VC2 = ....;
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects: VC1, VC2, nil];
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc]
initWithTarget:VC1
action:@selector(tap:)];
[tabBarController.view addGestureRecognizer:tapGR];
这正确地响应了点击方法“tap:”,但是 tabBarController 失去了对点击栏项目的本机点击响应。我尝试将手势添加到 tabBarController 中的视图控制器之一,如下所示:
[VC1.view addGestureRecognizer:tapGR];
但这样做后,点击手势根本无法识别,尽管 tabBar 对选项卡栏项目上的点击的本机点击识别被保留。
有人对如何解决此类问题有任何建议吗?我想一种方法是选择另一种手势,而不是点击来使用 tabBarController,但我真的不想这样做......
非常感谢您的观看!
I have a question on how to add a tap gesture to a UITabBarController. As the UITabBarController already has tap gestures built-in (responding to the tapping of the tab bar items on the tab bar), while technically I can add my own gesture to the tabBarController, the tabBar loses its own native tap gesture. Below is what I am trying to do:
UIViewController *VC1 = ....;
UIViewController *VC2 = ....;
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects: VC1, VC2, nil];
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc]
initWithTarget:VC1
action:@selector(tap:)];
[tabBarController.view addGestureRecognizer:tapGR];
This correctly responds to the tapping method "tap: ", but the tabBarController loses its native tapping responses to the tap bar items. I tried to add the gesture to one of the view controllers in the tabBarController like this:
[VC1.view addGestureRecognizer:tapGR];
but then doing it this way the tapping gesture is not recognized at all, although the tabBar's native tap recognition of the tapping on the tab bar items is retained.
Does anyone has any suggestions on how to resolve this type of issues? I guess one way is to pick another gesture other than tapping to go with tabBarController, but I'd really rather not do that....
Much thanks for viewing!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

我想知道你到底想用已经处理点击的控件上的点击做什么。请务必考虑您所做的事情是否会让您的用户感到困惑。
但如果必须,请尝试在手势识别器上将
cancelsTouchesInView
设置为 NO。除了由识别器处理之外,这还应该允许触摸传递到视图。I have to wonder what exactly you're trying to do with taps on a control that already handles taps. Do consider whether what you're doing is going to confuse your users.
But if you must, try setting
cancelsTouchesInView
to NO on the gesture recognizer. That should allow the touches to be passed on to the view in addition to being processed by your recognizer.