UIBarButton not working when toolbar superview has UITapGestureRecognizers? - iphone

I am experiencing this strange behavior.
I create a clean project (view template), add a toolbar with a button and hook it up with an action. it works ;)
BUT, then when I add a UITapGestureRecognizer to the view of my viewcontroller the toolbar button stops working. (It is pressed but its action is not called)
When I add the UITapGestureRecognizer only the action linked to it is called. It is like uitapgesture recognizer view were hiding the toolbar but actually is not.
What is happening here? What am I missing?
- (IBAction)itemAction{
NSLog(#"%s", _cmd);
self.view.backgroundColor = [UIColor whiteColor];
}
- (void) tapAction{
NSLog(#"%s", _cmd);
self.view.backgroundColor = [UIColor greenColor];
}
- (void)viewWasTapped:(UITapGestureRecognizer *)recognizer{
if (recognizer.state == UIGestureRecognizerStateRecognized) {
[self tapAction];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(viewWasTapped:)];
[tapGestureRecognizer setNumberOfTapsRequired:1];
[self.view addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];
}
EDIT: the project source can be downloaded from here
Thanks in advance for any advice
Ignacio

I've encountered a similar problem, and found this answer quite helpful, especially because it allows selectively excluding the gesture recognizer based on the type of view that was touched.

Eventually I found the reason:
tapGestureRecognizer.cancelsTouchesInView = NO;

Related

gesture recognizer doesn't work on subview

At first,I attach UISwipeGestureRecognizer to a image view,the action method can't be triggered.Then I attach UISwipeGestureRecognizer to view controller's view,it works well.I don't know why.
here is the code that doesn't work
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UISwipeGestureRecognizer *swipeRight=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRightAction)];
swipeRight.direction=UISwipeGestureRecognizerDirectionRight;
//imageView is an outlet of image view
[imageView addGestureRecognizer:swipeRight];
}
here is the code that works well
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UISwipeGestureRecognizer *swipeRight=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRightAction)];
swipeRight.direction=UISwipeGestureRecognizerDirectionRight;
//imageView is an outlet of image view
[self.view addGestureRecognizer:swipeRight];
}
Add this code to imageView:
imageView.userInteractionEnabled = YES;
Try this one
[imageView setUserInteractionEnabled:YES];

handle tap click on the background iphone

I have a view which contains different textfield.
Clicking on each taxtfield brings up a keyboard and if I click outside the textField the keyboard disappears but the view is also disappear! how can I change the code so that i can only get out of the view using cancel button and tapping down the view not clicking outside of the texfield or by clicking on the background.
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES];
self.view.backgroundColor = [self.appDel.styleManager appBackgroundGradientWithFrame:self.view.bounds];
self.tapView.backgroundColor = [UIColor clearColor];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(endPageEdit:)];
tap.delegate = self;
[self.view addGestureRecognizer:tap];
}
- (void)endPageEdit:(UIGestureRecognizer *)sender
{
if (sender.view == self.view) {
[self.view endEditing:YES];
if ([self isFormVisible]) {
[self handleTap:sender];
}
}
}
In your endPageEdit: method, tell the textFields to resignFirstResponder.
- (void)endPageEdit:(UIGestureRecognizer *)sender {
// assumes you have an outlet set up for each of the textFields
[self.textFieldA resignFirstResponder];
[self.textFieldB resignFirstResponder];
if (sender.view == self.view) {
[self.view endEditing:YES];
if ([self isFormVisible]) {
[self handleTap:sender];
}
}
}
Only zero or one of them will be the first responder, but there's no downside to resigning first responder when the control is not first responder. The reason the view is disappearing must have something to do with the endEditing: method or the handleTap method which you did not post.

UIButton Action Not calling using UITapGestureRecognizer iphone app?

Am working in iPhone applcation using UITapGestureRecognizer and UIButton. I have added UITapGestureRecognizer in self.view. When i click on the UIButton it won't call it's action the tapgesture action only calling. How to solve this issure? Can anyone please help to solve this issue?
Here is my code in -ViewDidLoad,
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapFrom:)];
[self.view addGestureRecognizer:tapRecognizer];
[tapRecognizer release];
UIButton *infoButton1 = [UIButton buttonWithType:UIButtonTypeInfoDark];
infoButton1.frame = CGRectMake(0, 5, 30, 30);
infoButton1.tag = 1;
infoButton1.opaque = YES;
[infoButton1 addTarget:self action:#selector(infoAlert:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
[toolBarView1 addSubview:infoButton1];
-(void) infoAlert:(id) sender
{
NSLog(#"Get Info Alert"); // Here the control not calling..
}
-(void)handleTapFrom:(UITapGestureRecognizer *)recognizer
{
NSLog(#"%#",[NSString stringWithFormat:#"%d", recognizer.view.tag]); // Whenever i touch the button this method only calling.
}
Please help me to solve this issue. Thanks in advance.
Why is no one using the wonderful search of stack overflow or Google ?
Here is your answer: https://stackoverflow.com/a/3348532/312312
And from the same thread, this seems to be even a simpler solution:
https://stackoverflow.com/a/8891030/312312
this is the best solution:
after you declared your tapRecognizer (i'm referring to your code on the question) add the following:
tapRecognizer.cancelsTouchesInView = NO;

UIImageView tap to hide navigation bar

I have a view which shows an image from the web. The view only has an UIImageView. I want to know how to hide the navigationBar when the user taps and show it again when the user re-taps the view again. (Just like the native iPhone photo app)
I know i can use this
[self.navigationController setNavigationBarHidden:YES animated:YES];
but i am not sure where to use this,where to put in this code.
Help would be appreciated
Initialize a new UITapGestureRecognizer:
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(toggleNavigationBar:)];
tapGestureRecognizer.numberOfTapsRequired = 1;
tapGestureRecognizer.numberOfTouchesRequired = 1;
[self.imageView addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];
You also must make sure the UIImageView has userInteractionEnabled set to YES because by default it is set to NO on UIImageView's.
self.imageView.userInteractionEnabled = YES;
Finally, write the method that is called when the gesture recognizer recognizes. This is the method selector that is passed in the action: argument in the gesture recognizer's initializer method:
- (void)toggleNavigationBar:(UITapGestureRecognizer *)tapGestureRecognizer
{
[self.navigationController setNavigationBarHidden:![self.navigationController isNavigationBarHidden] animated:YES];
}
Put a UITapGestureRecognizer on your UIImageView and in the delegate just call the method you mentioned. Something like this:
UITapGestureRecognizer* g = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
[img addGestureRecognizer:g];
[g release];
Then your delegate:
-(void) imageTapped:(UITapGestureRecognizer*)tg
{
if(self.navigationController.toolbarHidden)
[self.navigationController setNavigationBarHidden:YES animated:YES];
else
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
If you can't figure out the other answers you can cheat a little bit. You can throw on a button set it to transparent and link a IBAction to it with the code:
UIButton *imageButton = [[UIButton alloc] initWithFrame:CGRectMake( x,y,0,0)];
imageButton.backgroundColor = [UIColor clearColor];
[imageButton addTarget:self action:#selector(navBarHide:)
forControlEvents:UIControlEventTouchUpInside];
-(IBAction)navBarHide {
if (!navBarHidden) {
[self.navigationController.navigationBar removeFromSuperView];
}
else {
[YourUIView addSubview: yourNavigationBar];
}
}

I have a UITapGestureRecognizer added to an UIViewController's view that is in a UITableViewCell in a UITableView, why won't it fire?

The title is pretty explanatory, but I have a UITableView that I am populating with custom UITableViewCells.
Inside of these custom UITableViewCells, I am adding custom UIViewControllers that display custom images.
To this UIViewController's UIView, I am adding a UITapGestureRecognizer as follows:
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleTap:)];
recognizer.numberOfTapsRequired = 1;
recognizer.delegate = self;
[self.imageView addGestureRecognizer:recognizer];
[recognizer release];
}
-(void)handleTap:(UITapGestureRecognizer *)sender{
NSLog(#"Handling tap on ArticleTileViewController");
}
When I run the app, the cells are populating the images great, but when I tap on the image(or the custom UIViewController), nothing happens! My NSLog won't fire. I have looked over the code for an hour now, and don't see where I am going wrong.
Does anybody see something I'm missing? Or have they run into this before?
UIImageView objects have their userInteractionEnabled set to NO by default. This might be the case here as well. Add
self.imageView.userInteractionEnabled = YES;
to the viewDidLoad method that you've presented in the question.
If you are trying to get a button on each cell, here is a post on how to do it. Take a look.
iPhone SDK: Opening a cell with a dedicated button, not by cell tapping
- (void)viewDidLoad {
UILongPressGestureRecognizer* gesture = [[[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleLongPress:)] autorelease];
gesture.delegate = self;
[myWebView addGestureRecognizer:gesture];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}