UITapGestureRecognizer.numberOfTapsRequired = 2 not working - iphone

I added UITapGestureRecognizer for UIImageView in both single tap and double tap. for single tap method A will call and for double tap method B will call.
its working perfectly for first time only.
First Time
If i made single tap on UIImageView, method A called as expected.
If i made double tap on UIImageView, method B called as expected.
Second Time
If i made single tap on UIImageView, method A called as expected.
If i made double tap on UIImageView, method B not called again method A only called.
I do not know where i am making problem.
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideHandle:)];
[gestureRecognizer setDelegate:self];
[userResizableView addGestureRecognizer:gestureRecognizer];
[gestureRecognizer release];
UITapGestureRecognizer *gestureRecognizer1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
gestureRecognizer1.numberOfTapsRequired = 2;
[gestureRecognizer1 setDelegate:self];
[userResizableView addGestureRecognizer:gestureRecognizer1];
[gestureRecognizer1 release];
[gestureRecognizer requireGestureRecognizerToFail:gestureRecognizer1];

I have tested your code and it worked fine!
You are adding requireGestureRecognizerToFail after releasing gestureRecognizer1. This shouldn't be an issue as I tested after following comments and edited this answer but give it a go. Also give some meaningful names to variables and methods.
UITapGestureRecognizer *doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleDoubleTap:)];
doubleTapGestureRecognizer.numberOfTapsRequired = 2;
[userResizableView addGestureRecognizer:doubleTapGestureRecognizer];
UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleSingleTap:)];
singleTapGestureRecognizer.numberOfTapsRequired = 1;
[singleTapGestureRecognizer requireGestureRecognizerToFail: doubleTapGestureRecognizer];
[userResizableView addGestureRecognizer:singleTapGestureRecognizer];
[doubleTapGestureRecognizer release];
[singleTapGestureRecognizer release];

Related

UIWebview Gesture iOS

How can I identify the user touch, tap & double tap in the UIWebview. Is there any delegates available like touches begin etc?
here is the code to implement the single tap and double tap on webview
UITapGestureRecognizer *singleTap = [[[UITapGestureRecognizer alloc] initWithTarget: self action:#selector(doSingleTap)] autorelease];
singleTap.numberOfTapsRequired = 1;
[self.myWebView addGestureRecognizer:singleTap];
UITapGestureRecognizer *doubleTap = [[[UITapGestureRecognizer alloc] initWithTarget: self action:#selector(doDoubleTap)] autorelease];
doubleTap.numberOfTapsRequired = 2;
[self.myWebView addGestureRecognizer:doubleTap];
The below 3 links will solve your problem
how-to-add-a-gesture-recognizer-to-a-uiwebview-subclass
iphone-custom-gestures-on-your-uiwebview
does-uigesturerecognizer-work-on-a-uiwebview
About number of taps, you can set it through the property numberOfTapsRequired
Eg:
UITapGestureRecognizer *myGusture = [[[UITapGestureRecognizer alloc] initWithTarget: self action:#selector(doTap)] autorelease];
myGusture.numberOfTapsRequired = 1; //you can change it to any number as per your requirement.
[self.myWebView addGestureRecognizer:myGusture];
[myGusture release];
myGusture = nil;

Two UITapGestureRecognizer in on UIView

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

iPhone - 3 multiple detections of UIGestureRecognizer

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

UILongPressGestureRecognizer of Superview gets called instead of Subview

i got a strange problem. I got a Superview with customcontrols as subviews.
Their are GestureRecognizer both in superview and subviews.
If i tap a subview its GestureRecognizer gets called and on tap on the superview its tap gets called.
but on long press in the subview SOMETIMES the GestureRecognizer of the superview gets called. I add the GestureRecognizers in the same functions but there is a different attitude.
Superview
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapPiece:)];
tapGesture.numberOfTapsRequired = 1;
//contenView is the area where my controls can be
[self.contentView addGestureRecognizer:tapGesture];
[tapGesture release];
UILongPressGestureRecognizer *longTapGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longTapPiece:)];
tapGesture.numberOfTapsRequired = 1;
[self.contentView addGestureRecognizer:longTapGesture];
[longTapGesture release];
Subviews
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(selectPiece:)];
tapGesture.numberOfTapsRequired = 1;
[self addGestureRecognizer:tapGesture];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPiece:)];
longPressGesture.numberOfTapsRequired = 1;
[self addGestureRecognizer:longPressGesture];
Can anyone tell me why the longtap doen't response to my subview and how to fix it.
THANK YOU
got a solution but this is not what i wanted i set the duration of the control lower than the one of the superview [longPressGesture setMinimumPressDuration:0.4]; But the gestureRecognizer should be independend

Detecting taps in uiimageview inside uiscrollview

I have a UIScrollView with multiple UIImageViews in it created like this.
frame = [[UIImageView alloc] initWithImage:bg];
frame.frame = CGRectMake(FRAME_SEPARATOR + numPage*1024 + numColumn*(FRAME_SEPARATOR+230), 10 +numRow*(FRAME_SEPARATOR+145), 230, 145);
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
[frame addGestureRecognizer:tap];
[tap release];
[scroll addSubView:frame];
The problem is that imageTapped is not being called when tapping over an image.
If I add the gesture recognizer to the scrollview like this:
UITapGestureRecognizer *tap =
[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
[scroll addGestureRecognizer:tap];
[tap release];
imageTapped is called.
How can I detect the taps over the UIImageViews?
Thanks
Make sure userInteractionEnabled is set to YES on the UIImageView:
frame.userInteractionEnabled = YES;
I'd also recommend using a different name for the UIImageView variable (eg. imageView instead of frame). Otherwise, you can easily confuse it with the view's frame property.