[英]How to determine whether the user touched the toolbar item or not?
我有一个工具栏,我在其中添加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);
}]
}
“但按钮动作没有表现。任何人都可以帮助我找出我哪里出错了?
可能是这个解决方案可以帮助你......! 你可以将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;
}
访问此referance: -
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
}
手势识别器设置在工具栏上,因此发送者始终是工具栏,这意味着
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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.