簡體   English   中英

長按UITabBarItem后禁用切換選項卡

[英]Disable switching tabs after long press on UITabBarItem

我想禁用UITabBarViewController的切換到長按能力UITabBarItem在特定的tag

我試過的是

  1. UITabBarViewController子類化為UIGestureRecognizerDelegate
  2. 添加了UILongPressGestureRecognizer並將其delegate設置為self
  3. 覆蓋的gestureRecognizerShouldBegin ,使其返回NO

但這沒有用。

請注意,我已經將UITapGestureRecognizer *recognizer添加到UITabBarItem之一,如下所示:

[self.tabBar.subviews[2] addGestureRecognizer:recognizer]

而且效果很好。 我只想禁用識別長按並立即觸發UITapGestureRecognizer ,即使長按也是如此。

謝謝

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
                                             initWithTarget:self 
                                             action:@selector(handleLongPress:)];
        longPress.minimumPressDuration = 0.5;
        [self addGestureRecognizer:longPress];

並在手柄上長按方法

-  (void)handleLongPress:(UILongPressGestureRecognizer*)sender { 
    if (sender.state == UIGestureRecognizerStateEnded) {
      NSLog(@"UIGestureRecognizerStateEnded");
    //Do Whatever You want on End of Gesture
[[[[self.tabBarController tabBar]items]objectAtIndex:0]setEnabled:FALSE];
     }
    else if (sender.state == UIGestureRecognizerStateBegan){
       NSLog(@"UIGestureRecognizerStateBegan.");
   //Do Whatever You want on Began of Gesture
     }
  }

didFinishLaunchingWithOptions: AppDelegate成為UITabBarControllerDelegate didFinishLaunchingWithOptions:

   UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    tabBarController.delegate = (id)self;

並添加此方法:

 - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController; 
{    
       if (viewController.restorationIdentifier isEqualToString:@"foo")
           return YES;
       else
           return NO;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM