Reputation: 8957
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 `
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];
`
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?
Upvotes: 0
Views: 214
Reputation: 49730
might be this Solution helps you..! you can isKindOfClass as bellow in to your Gesture Action.
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
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldReceiveTouch:(UITouch *)touch {
if (touch.view == buttonGoForward) {
return NO;
}
if (touch.view == buttonGoBack) {
return NO;
}
return YES;
}
visit this referance:-
Does Button Tap Event Get Overridden by Tap Gesture Recognizer?
Upvotes: 1
Reputation: 12641
UIGesture View delegate let u ignore touch when UIBarButtonItem is clicked and clicking on UIToolBar will hide your toolbar as u have coded:-
gestureRecognizer.delegate=self;
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if (![touch.view isKindOfClass:[UIToolbar class]]) {
return NO;
}
return YES; // handle the touch
}
Upvotes: 1
Reputation: 9484
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.
[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
}
}
Upvotes: 0