Hi, I am trying to add gesture recognizers to 'UIButton'. When I do it like this:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[self.LeftBottomSpaceBtn addGestureRecognizer:singleTap];
[singleTap requireGestureRecognizerToFail:doubleTap];
[singleTap release];
It works properly, but when I tried to add this gesture to multiple buttons it did not work:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[self.LeftBottomSpaceBtn addGestureRecognizer:singleTap];
[self.LeftUpSpaceBtn addGestureRecognizer:singleTap];
[self.RightBUpSpaceBtn addGestureRecognizer:singleTap];
[self.LeftReturnBtn addGestureRecognizer:singleTap];
[self.RightReturnBtn addGestureRecognizer:singleTap];
[self.DeleteBtn addGestureRecognizer:singleTap];
[self.CapsBtn addGestureRecognizer:singleTap];
[singleTap requireGestureRecognizerToFail:doubleTap];
[singleTap release];
So how can I add the same gesture to multiple buttons in the same way I have added the 'longPress' and 'doubleTap'?
I'd suggest the following:
NSMutableSet *buttons = [[NSMutableSet alloc] init];
[buttons addObject: self.LeftBottomSpaceBtn];
[buttons addObject: self.LeftUpSpaceBtn];
[buttons addObject: self.RightBUpSpaceBtn];
[buttons addObject: self.LeftReturnBtn];
[buttons addObject: self.RightReturnBtn];
[buttons addObject: self.DeleteBtn];
[buttons addObject: self.CapsBtn];
for(UIButton *button in buttons)
{
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[button addGestureRecognizer:singleTap];
[singleTap requireGestureRecognizerToFail:doubleTap];
[singleTap release];
}
If you save the set as a variable, you can do other stuff for all buttons as well, such as releasing them all and changing all their backgroundColors, without calling them all individually.
You will probably need to make seperate doubletaprecognizers for each button as well.
You can add one gesture recognizer to one view alone. If you add it to more than one views, the last added view will be added with the recognizer.
Create different instances of gesture recognizers and add them to individual views.
Related
I am using Apple code for just zoom the UIImageView on taps and gestures. But its not work ?
Please see this Apple's link apple code for image zooming by taps and gestures
-(void)veiwDidLoad
{
[super viewDidLoad];// Edited my self
imageView.userInteractionEnabled = YES; // Edited my self
//All code below same.... like Apple's code
// set the tag for the image view
[imageView setTag:ZOOM_VIEW_TAG];
// add gesture recognizers to the image view
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleTap:)];
UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTwoFingerTap:)];
[doubleTap setNumberOfTapsRequired:2];
[twoFingerTap setNumberOfTouchesRequired:2];
[imageView addGestureRecognizer:singleTap];
[imageView addGestureRecognizer:doubleTap];
[imageView addGestureRecognizer:twoFingerTap];
[singleTap release];
[doubleTap release];
[twoFingerTap release];
// calculate minimum scale to perfectly fit image width, and begin at that scale
float minimumScale = [imageScrollView frame].size.width / [imageView frame].size.width;
[imageScrollView setMinimumZoomScale:minimumScale];
[imageScrollView setZoomScale:minimumScale];
}
Make sure that you set your imageView to be userInteractionEnabled = YES; .. its assign to NO by default.
change the function name from
-(void) veiwDidLoad
to
-(void) viewDidLoad
I has a problem like that as well, does that fix it?
I'm using two different TapGestureRecognizer to handle both single and double tap on screen.
This is the code:
UITapGestureRecognizer *tapGR =[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(handleTap:)];
[tapGR setDelegate:self];
[tapGR setNumberOfTapsRequired:1];
[self addGestureRecognizer:tapGR];
UITapGestureRecognizer *doubleTapGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(handleDoubleTap:)];
[doubleTapGR setNumberOfTouchesRequired:2];
[self addGestureRecognizer:doubleTapGR];
[tapGR requireGestureRecognizerToFail : doubleTapGR];
[tapGR release];
[doubleTapGR release];
Even if I specified that [tapGR requireGestureRecognizerToFail: doubleTapGR] the "handleTap" selector is performed. Where is the mistake?
You made a mistake! You used setNumberOfTouchesRequired method for doubleTapGR instead of using setNumberOfTapsRequired. Here is the corrected code:
UITapGestureRecognizer *tapGR =[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(handleTap:)];
[tapGR setDelegate:self];
[tapGR setNumberOfTapsRequired:1];
[self addGestureRecognizer:tapGR];
UITapGestureRecognizer *doubleTapGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(handleDoubleTap:)];
[doubleTapGR setNumberOfTapsRequired:2];
[self addGestureRecognizer:doubleTapGR];
[tapGR requireGestureRecognizerToFail : doubleTapGR];
[tapGR release];
[doubleTapGR release];
Best regards ;)
I think you may have omitted:
[doubleTapGR setDelegate:self];
I figured out that I really am dumb :)
I wrote:
[doubleTapGR setNumberOfTouchesRequired:2];
Instead of:
[doubleTapGR setNumberOfTapsRequired:2];
Feel free to delete this answer. Sorry StackOverflow.
I want to add to my UIViewController :
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:tapGesture];
[tapGesture release];
UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture2:)];
tapGesture2.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tapGesture2];
[tapGesture2 release];
the problem is if the user tap twice the two methods are called,and i want that if the user make double tap only the first(handleTapGesture) will be called and if he make one tap it will call only the second one(handleTapGesture2)
use this one..
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture)];
tapGesture.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:tapGesture];
[tapGesture release];
UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture2)];
tapGesture2.numberOfTapsRequired = 1;
[tapGesture2 requireGestureRecognizerToFail: tapGesture];
[self.view addGestureRecognizer:tapGesture2];
[tapGesture2 release];
you can use the code i posted on here
in this the method requireGestureRecognizerToFail: is used in viewcontroller.m this will solve your problem
I have 3 UIGestureRecognizers attached to a view: one finger double tap, two fingers double tap and one finger triple tap. The problem is this: when I triple tap, it fires the method that should be fired by double tap then the method for triple tap.
This is how I added it.
// one finger double tap
doubleTap = [[[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleDoubleTap:)] autorelease];
[doubleTap setCancelsTouchesInView:YES];
[doubleTap setNumberOfTapsRequired:2];
[doubleTapDoisDedos setNumberOfTouchesRequired:1];
[doubleTap setDelegate:self];
[[[CCDirector sharedDirector] openGLView] addGestureRecognizer:doubleTap];
// two fingers double tap
twoFingerDoubleTap = [[[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleDoubleTapTwoFingers:)] autorelease];
[twoFingerDoubleTap setCancelsTouchesInView:YES];
[twoFingerDoubleTap setNumberOfTapsRequired:2];
[twoFingerDoubleTap setNumberOfTouchesRequired:2];
[twoFingerDoubleTap setDelegate:self];
[[[CCDirector sharedDirector] openGLView] addGestureRecognizer:twoFingerDoubleTap];
// triple tap com um dedo faz as cartas se empilharem
tripleTapOneFinger = [[[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleTripleTap:)] autorelease];
[tripleTapOneFinger setCancelsTouchesInView:YES];
[tripleTapOneFinger setNumberOfTapsRequired:3];
[tripleTapOneFinger setNumberOfTouchesRequired:1];
[tripleTapOneFinger setDelegate:self];
[[[CCDirector sharedDirector] openGLView] addGestureRecognizer:tripleTapOneFinger];
and yes, I have tried to add these, without success
[doubleTap requireGestureRecognizerToFail:doubleTapTwoFingers];
[doubleTap requireGestureRecognizerToFail:tripleTapOneFinger];
[doubleTapTwoFingers requireGestureRecognizerToFail:doubleTap];
[doubleTapTwoFingers requireGestureRecognizerToFail:tripleTapOneFinger];
[tripleTapOneFinger requireGestureRecognizerToFail:doubleTap];
[tripleTapOneFinger requireGestureRecognizerToFail:doubleTapTwoFingers];
what am I missing?
is there a way to, inside the handle method, detect the number of taps?
thanks
Adding too many gesture recognizer dependancies through requireGestureRecognizerToFail: will cause problems. Just add the one dependency that is needed, in your case:
[doubleTap requireGestureRecognizerToFail:tripleTapOneFinger];
all of you. I have been working on map view but i unable solve pinch and tap detection problem.I want to detect pinch and tap in map view.
I have tried following code in iPhone MapView
UITapGestureRecognizer *Tap= [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(checktap)];
[self.mapView addGestureRecognizer:Tap];
[Tap release];
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:#selector(checkpinch)];
[self.mapView addGestureRecognizer:pinch];
[pinch release];
Where tap is working but pinch detection is not working.
Please help me.
Thanks in Advanced.
Add following code in your view did load and add Gesture recognizer delegate in you .h file.
Enjoy...:)
[self.view insertSubview:mapView atIndex:0];
//Gesture reconizer
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:#selector(checkpinch)];
[pinch setDelegate:self];
[pinch setDelaysTouchesBegan:YES];
[self.mapView addGestureRecognizer:pinch];
[pinch release];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(checktap)];
[singleTap setDelegate:self];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired =2;
[self.mapView addGestureRecognizer:singleTap];
[singleTap release];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(checktap)];
[doubleTap setDelegate:self];
doubleTap.numberOfTapsRequired = 2;
doubleTap.numberOfTouchesRequired =1;
[self.mapView addGestureRecognizer:doubleTap];
[doubleTap release];