iphone - 如何判断用户是否触摸了工具栏项?

标签 iphone ios ipad

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

                      }]
}

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

最佳答案

UIGesture View 委托(delegate)让您在单击 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
}

关于iphone - 如何判断用户是否触摸了工具栏项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17674648/

相关文章:

iphone - 如何正确更新Three20?

iphone - 使用 UILabels 以及其中的文本创建自定义 UITextView

iphone - ASIHTTPRequest gzip 解压

ios - 制作链式动画的正确方法

ios - 横屏模式的zxing阅读器

ios - Mobclix 不会打开? (错误代码 -9999999)

iphone - 仅使用来自不同目标的类的一部分

ios - 多个 block 数据调用完成时如何通知

iphone - 自定义 uitableviewcell 内的按钮不起作用

iphone - 循环计数器 - 最短方法