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