如何确定用户是否触摸了工具栏项?

时间:2013-07-16 10:52:10

标签: iphone ios ipad

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

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);   

                      }]
}

` 但按钮操作没有执行。任何人都可以帮我找出我错在哪里?

3 个答案:

答案 0 :(得分:1)

可能是这个解决方案可以帮助你......!您可以 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委托但我需要编辑这个像bellow

- (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?

答案 1 :(得分: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 :(得分:0)

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

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
}
}