UIButton Action Not calling using UITapGestureRecognizer iphone app? - iphone

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;

Related

UIButton TouchUpInside not being fired, but pointInside:withEvent: returns YES

I have a UIButton on which I attached an UIControlEventTouchUpInside action. This button is inserted in a UIScrollView.
My problem is: touch events won't work until I hold on the button for a certain time. If I tap on it directly, or just click on it in the simulator, it won't work.
The thing is: when I just click on it, pointInside:withEvent: is actually triggered and returns YES. What could possibly prevent the event from being fired?
Try setting the delaysContentTouches property of your scrollView to NO.
It should work so you can check the following eg.:
1) make sure that you button is properly added to scrollview. For example like this:
...
myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
myScrollView.bounces = NO;
[myScrollView setScrollEnabled:YES];
myScrollView.showsHorizontalScrollIndicator = NO;
myScrollView.delegate = self;
myScrollView.pagingEnabled = YES;
[self.view addSubview:myScrollView];
myScrollView.contentSize = CGSizeMake(320*5, 480);
...
UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeCustom];
mybutton.frame = CGRectMake(0,0,100,100);
mybutton.tag = 1
[mybutton addTarget:self action:#selector(pressedmybutton:)
forControlEvents:UIControlEventTouchUpInside];
[myScrollView addSubview:bu];
2) make sure that button is top of the view stack. For testing purpose add at the end of the view init code [mybutton bringSubviewToFront];
3) test it on device and not just in Simulator. I experienced different scrolling/button pressing behavior in Simulator than on real device (which is the better for testing this out).

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];
}
}

Touch event in app delegate iOS

How to achieve touch even for UIImageView in appDelegate?
You can use:
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(anyMethod)];
[imageView addGestureRecognizer:tgr];
[tgr release];
Before doing this, enable user interaction of UIimageView.
This might be helpful for you:
http://mithin.in/2009/08/26/detecting-taps-and-events-on-uiwebview-the-right-way/

UIBarButton not working when toolbar superview has UITapGestureRecognizers?

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;

How to create a new UIView programmatically in cocos2d?

I want to create a new UIView on button click programmatically in cocos2d. Kindly help me with some sample code. Thanks in advance.
You have to create a button like-
UIButton* myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(30, 70,100,38); //set frame for button
[myButton setTitle:#"subview" forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];
Or you can use CCMenu as a button.
And then write the event handling function-
-(void)buttonClicked:(id)sender
{
UIView *myview=[[UIView alloc] initWithFrame: CGRectMake(0, 0,320,480)];
myview.backgroundColor=[UIColor redColor];
[[[CCDirector sharedDirector] openGLView] addSubview:myview];
[myview release];
}
}
As you wrote above, the class is called UIView. It's really simple to create a new View. Take a look at this code snippet.
-(void)youButtonAction:(id)sender {
//This method is executed as soon as you button is pressed.
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
[anotherView addSubview:newView]; //Add the newView to another View
[newView release];
}
I hope you got the point. At first you have to create the view, than add it to another view and in the end release it. ;-)
Sandro Meier