【问题标题】:How to determine whether the user touched the toolbar item or not?如何判断用户是否触摸了工具栏项?
【发布时间】:2013-07-16 10:52:10
【问题描述】:

我有一个工具栏,我在其中添加了 3 个按钮作为 UItoolbaritems。现在我还在工具栏中添加了一个手势识别器以显示和隐藏该工具栏。但我需要区分触摸是否来自按钮或在外面执行我的功能。我试过这个`

self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;
    self.navigationController.toolbar.frame=CGRectMake(0, [[UIScreen mainScreen] bounds].size.height -12, [[UIScreen mainScreen] bounds].size.width, 44);

    self.navigationController.toolbar.tintColor=[UIColor colorWithRed:40/255.0 green:40/255.0 blue:40/255.0 alpha:1.0];  


    buttonGoBack = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back_icon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backButtonTouchUp:)];

buttonGoBack.tag=1;

    UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    fixedSpace.width = 30;


    buttonGoForward = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"forward_icon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(forwardButtonTouchUp:)];
    buttonGoForward.tag=2;

    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    [self.navigationController.toolbar addGestureRecognizer:gestureRecognizer];



      NSMutableArray *toolBarButtons = [[NSMutableArray alloc] init];

    [toolBarButtons addObject:buttonGoBack];
    [toolBarButtons addObject:fixedSpace];
    [toolBarButtons addObject:buttonGoForward];

`

我已经将手势识别器作为`

 if(sender.view.tag==1)
           [self performSelector:@selector(backButtonTouchUp:) withObject:nil];
        else if(sender.view.tag==2)
           [self performSelector:@selector(forwardButtonTouchUp:) withObject:nil];
         else
          {
     [UIView animateWithDuration:.50 
                      animations:^{
         if(self.navigationController.toolbar.frame.origin.y==[[UIScreen mainScreen] bounds].size.height -12)
                         self.navigationController.toolbar.frame=CGRectMake(0, [[UIScreen mainScreen] bounds].size.height -44, [[UIScreen mainScreen] bounds].size.width, 44);
         else
                            self.navigationController.toolbar.frame=CGRectMake(0, [[UIScreen mainScreen] bounds].size.height -12, [[UIScreen mainScreen] bounds].size.width, 44);   

                      }]
}

` 但是按钮操作没有执行。谁能帮我找出我哪里出错了?

【问题讨论】:

    标签: iphone ios ipad


    【解决方案1】:

    UIGesture View 委托让你在点击 UIBarButtonItem 时忽略触摸,点击 UIToolBar 将隐藏你的工具栏,因为你已经编码:-

     gestureRecognizer.delegate=self;
    
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
    {
        if (![touch.view isKindOfClass:[UIToolbar class]]) {
            return NO;
        }
        return YES; // handle the touch
    }
    

    【讨论】:

      【解决方案2】:

      也许这个解决方案可以帮助你..!您可以在手势操作中isKindOfClass 如下所示。

      例如,您可以使用以下方法:-

       NSArray *subs = [self.yourviewcontroller.view subviews];
          for (UIToolbar *child in subs) {
              if ([child isKindOfClass:[UIToolbar class]]) {
      
                  for (UIView *v in [child items])
                  {
                      if ([v isKindOfClass:[UIBarButtonItem class]])
                      {
                          UIBarButtonItem *b = (UIBarButtonItem*)v;
      
                          NSLog(@"found");
                          //do something with b you can also fond your clickable Barbutton Item.
                      }
                  }
      
              }
          }
      

      或建议的其他解决方案,但 Herçules 代表但我需要像下面这样编辑它

      - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
                  shouldReceiveTouch:(UITouch *)touch {
      
          if (touch.view == buttonGoForward) {
              return NO;
          }
          if (touch.view == buttonGoBack) {
              return NO;
          }
      
          return YES;
      }
      

      访问此参考:-

      Does Button Tap Event Get Overridden by Tap Gesture Recognizer?

      【讨论】:

        【解决方案3】:

        手势识别器设置在工具栏上,因此发送者始终是工具栏, 这意味着

        if(sender.view.tag==1) 
        

        这种情况是行不通的。 你可以做的是使用 UIView hitTest:withEvent 方法或 为每个按钮使用一个动作/选择器, 那么您可以使用单个按钮的标签来了解哪个栏按钮调用了该操作。

        [buttonGoBack setAction:@selector(buttonTapped:)]
        [fixedSpace setAction:@selector(buttonTapped:)];
        [buttonGoForward setAction:@selector(buttonTapped:)];
        
        - (IBAction)buttontapped:(id)sender{
        if(sender.tag == 0)
        {
          // perform some action
        }
        else if(sender.tag == 1){
          // perform some action
        }
        }
        

        【讨论】:

        • 查看我的编辑...如果我正确理解了您的问题,那么这应该对您有所帮助。
        • 这里的问题是如何从我的识别器操作中获取按钮
        • 您需要通过在工具栏上设置手势识别器而不是在单个栏按钮项上设置操作来做到这一点?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-09
        • 2023-03-02
        • 2013-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多