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];
Related
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];
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?
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];
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.
For UITapGestureRecognizer, you can set number of taps required to control the recognition of the UITapGestureRecognizer. If you set numberOfTapsRequired to 2 and user taps only once, then the UITapGestureRecognizer won't be triggered.
My question is How about for UIPanGestureRecognizer? How to control its recognition?
I have a view. Once I set a UIPanGestureRecognizer to it, any dragging will trigger the action. But what I want is only the dragging in X-axis. And for non-X-axis dragging, all touch events should be sent to other views underneath.
How can I do it?
THanks
Set its delegate and implement
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
Then use
- (CGPoint)velocityInView:(UIView *)view;
on the gesture recogniser to calculate whether the gesture recognizer should handle it or not.
Check How to stop UIPanGestureRecognizer when object moved to certain frame. It may help you.
UIView *holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
UIImageView *imageview = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imageview setImage:image];
[holderView addSubview:imageview];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[holderView addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[holderView addGestureRecognizer:tapRecognizer];
[self.view addSubview:holderView];
Raees
Assume that your gestureRecognizer triggers the action _panRecogPanned below. You can see how the center of a subview ( the view that carries the gesture recognizer itself) moves following the transition. To disable panning on y axis, you simply set the center as the calculated new center, whereas the translation.y is omitted.
To move other subviews on the y axis, get their frame, update their origin.x property and reset the frame, they should follow your finger only on the y axis.
- (IBAction)_panRecogPanned:(id)sender{
CGPoint translation = [_panRecog translationInView:_statementFilterView];
//This subview only moves horizontally
_panRecog.view.center = CGPointMake(translation.x + _panRecog.view.center.x, _panRecog.view.center.y);
//This subview only moves vertically
CGRect newFrame = anotherSubview.frame;
newFrame.origin.y = anotherSubview.frame.origin.y + translation.y;
anotherSubview.frame = newFrame;
[_panRecog setTranslation:CGPointMake(0, 0) inView:self.view];
}