简体   繁体   English

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

[英]How to determine whether the user touched the toolbar item or not?

I had a toolbar in which i am adding 3 buttons as UItoolbaritems.Now i am also adding a gesture recogniser also to the toolbar to show and hide that toolbar.But I need to distinguish betwwen whether the touch is coming from the button or outside to carry through my functions.I tried this ` 我有一个工具栏,我在其中添加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; 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];

` `

and i had done the gesture recogniser as ` 我把手势识别器做成了`

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

                      }]
}

` But the buttons actions are not performing.Can anybody help me to find out where i am going wrong? “但按钮动作没有表现。任何人都可以帮助我找出我哪里出错了?

might be this Solution helps you..! 可能是这个解决方案可以帮助你......! you can isKindOfClass as bellow in to your Gesture Action. 你可以将isKindOfClass看作你的手势动作。

for example you can do it with bellow:- 例如,你可以用以下方式做到: -

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

        }
    }

Or other solution as suggest but Herçules delegate But i need edit this like bellow 或其他解决方案作为建议,但Herçules委托但我需要编辑这个像波纹管

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
            shouldReceiveTouch:(UITouch *)touch {

    if (touch.view == buttonGoForward) {
        return NO;
    }
    if (touch.view == buttonGoBack) {
        return NO;
    }

    return YES;
}

visit this referance:- 访问此referance: -

Does Button Tap Event Get Overridden by Tap Gesture Recognizer? 按键点击事件是否被Tap手势识别器覆盖?

UIGesture View delegate let u ignore touch when UIBarButtonItem is clicked and clicking on UIToolBar will hide your toolbar as u have coded:- 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
}

The gesture recogniser is set on toolbar so the sender always is toolbar, it means 手势识别器设置在工具栏上,因此发送者始终是工具栏,这意味着

if(sender.view.tag==1) 

this condition will not work. 这种情况不起作用。 What you can do instead is to use UIView hitTest:withEvent method or use one action/selector for every button, then you can use tags of individual button to know which bar button has called that action. 你可以做的是使用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
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM