开发者

adding tap gesture to a tabBarController

开发者 https://www.devze.com 2023-02-22 01:09 出处:网络
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), whil

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 recog开发者_如何学Pythonnized 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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消