iphone - 点击 UITabbar 按钮上的手势

标签 iphone ios uitabbarcontroller uigesturerecognizer uitapgesturerecognizer

我用标签栏 Controller 创建了一个项目。我的标签上有 5 个按钮,现在我想在我的第一个标签上做双击手势识别

我已经实现了在标签栏上做手势,但是当我双击标签栏上的任何按钮时,该方法被调用。 如何使其仅适用于单个 tabbarbutton?

 -(void)createTab{

     Array = [[NSMutableArray alloc] initWithCapacity:5];

    myhomeVC = [[MyHomeViewController alloc] initWithNibName:@"MyHomeViewController" bundle:nil];
 homeNavBar=[[UINavigationController alloc]initWithRootViewController:myhomeVC];

     groupVC = [[GroupSearchViewController alloc] initWithNibName:@"GroupSearchViewController" bundle:nil];
    groupNavBar=[[UINavigationController alloc]initWithRootViewController:groupVC];

    uploadVC = [[UploadFoodImageViewController alloc] initWithNibName:@"UploadFoodImageViewController" bundle:nil];
    uploadNavBar=[[UINavigationController alloc]initWithRootViewController:uploadVC];

    searchVC = [[SearchViewController alloc] initWithNibName:@"SearchViewController" bundle:nil];
    searchNavBar=[[UINavigationController alloc]initWithRootViewController:searchVC];

    nearbyVC = [[NearByViewController alloc] initWithNibName:@"NearByViewController" bundle:nil];
    nearbyNavBar=[[UINavigationController alloc]initWithRootViewController:nearbyVC];



    [Array addObject:homeNavBar];
    [Array addObject:groupNavBar];
    [Array addObject:uploadNavBar];
    [Array addObject:searchNavBar];
    [Array addObject:nearbyNavBar];

    appDelegate.tabBarController.viewControllers =Array;


     UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
    [doubleTap setNumberOfTapsRequired:2];
    [appDelegate.tabBarController.view addGestureRecognizer:doubleTap];


}




 -(void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {
    NSLog(@"Tab tpped");
  }

最佳答案

我有一个技巧可以做到这一点。你也试试,可能有用。我仍在努力。

转到 shouldSelectViewController 委托(delegate)方法。每次点击项目时都会触发此方法。所有这些 switch 语句的情况都是应用程序的不同选项卡点击。

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    DBGS;
    switch (viewController.tabBarItem.tag)
    {
        case 0:
        {
            _tapCounter++; // Here I am increasing a NSInteger count to check how many times it increaed
            if(_tapCounter == 2) // it is tapped twice, so select this case
            {
                DBG(@"Double Tap done");
                [[(AppDelegate *)[[UIApplication sharedApplication] delegate] fvc] getFeeds]; // write your code that you want on double tap
                _tapCounter = 0; // making it zero again
            }
            [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(tapZero) userInfo:self repeats:NO]; // this timer waits for 1 sec (bacause 1 sec is long and double tap should be in few miliseconds), then again makes _tapCounter to zero.
        }
            break;
        case 1:
        {

        }
            break;
        case 2:
        {

        }
            break;
        case 3:
        {

        }
        case 4:
        {

        }
            break;
        default:
            break;
    }
    return YES;
}

- (void)tapZero
{
    _tapCounter = 0; // _tapCounter is zero here
}

关于iphone - 点击 UITabbar 按钮上的手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16097199/

相关文章:

ios - 使用 CATransform3D 性能不佳

iphone - 客户向我发送了他的分发证书和私钥,以便我可以推送到应用商店,但它们不与自身关联

ios - 我们如何将验证码集成到 iOS 应用程序中?

ios - "Presenting a controller modally within a nav controller within a tab bar controller"的问题

objective-c - 当 tabBarController/navController 在同一应用程序中时推送到详细 View 时出现问题

iphone - 涉及NSString的内存泄漏

javascript - 有没有办法将视口(viewport)的比例动态重置为 1.0

iphone - iOS 版 Twitter 入门

iphone - 一个基本的 UITabBar 应用程序

iphone - 将 iPhone xib 应用程序自动调整为 ipad