0

我有一个工具栏,我在其中添加了 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);   

                      }]
}

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

4

3 回答 3

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 代表但我需要像下面这样编辑

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

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

    return YES;
}

访问此参考:-

点击手势识别器是否会覆盖按钮点击事件?

于 2013-07-16T12:52:30.160 回答
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
}
于 2013-07-16T12:59:57.247 回答
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
}
}
于 2013-07-16T11:01:00.920 回答