I have a UITapGestureRecognizer added to my UITextView. I have set the numberOfTapsRequired to 1, but I really need this to be ANY. aField is a UITextView. I need any number of taps to call the selector. Any way to do this?
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(stateFieldSelected)];
singleTap.numberOfTapsRequired = 1;
[aField addGestureRecognizer:singleTap];
The purpose is, I am launching a UIPickerView when they tap the State field, but if they double or triple tap the state field, it will pull up the keyboard. I could add many gesture recognizers, but it seems lame to have to do that.
There isn't anything wrong with using multiple gesture recognizers on a view. But if you're worried about the single tap action being called twice in addition to the double tap action you could specify that the single tap recognizer requires the double tap recognizer to fail:
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(someOtherMethod)];
[doubleTap setNumberOfTapsRequired:2];
[aField addGestureRecognizer:doubleTap];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(stateFieldSelected)];
[singleTap setNumberOfTapsRequired:1];
[singleTap requireGestureRecognizerToFail:doubleTap];
[aField addGestureRecognizer:singleTap];
Related
I have three UIImageview. I set the action for UIImageview by using tapGesture. I want each UIImageview to have a different action. How can I check that which UIImageview is presently clicked?
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapping:)];
[singleTap setNumberOfTapsRequired:1];
[_imageOne addGestureRecognizer:singleTap];
[_imageTwo addGestureRecognizer:singleTap];
[_imageThree addGestureRecognizer:singleTap];
-(void)singleTapping:(UIGestureRecognizer *)recognizer {
}
You can detect using below code:
-(void)singleTapping:(UITapGestureRecognizer *) recognizer{
if (recognizer.view == _imageOne){
} else if (recognizer.view == _imageTwo) {
}else
{
}
}
A UIGestureRecognizer is to be used with a single view. There is this explicit information in the Apple documentation:
Gesture Recognizers Are Attached to a View
Every gesture recognizer is associated with one view. By contrast, a
view can have multiple gesture recognizers, because a single view
might respond to many different gestures. For a gesture recognizer to
recognize touches that occur in a particular view, you must attach the
gesture recognizer to that view.
Firstly, you can't attach same UITapGestureRecognizer to more than one view.
So, your code will not work and you have to create three UITapGestureRecognizer instances and have to attach it to views like below:
UITapGestureRecognizer *singleTapImg1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapping:)];
[singleTapImg1 setNumberOfTapsRequired:1];
UITapGestureRecognizer *singleTapImg2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapping:)];
[singleTapImg2 setNumberOfTapsRequired:1];
UITapGestureRecognizer *singleTapImg3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapping:)];
[singleTapImg3 setNumberOfTapsRequired:1];
[_imageOne addGestureRecognizer:singleTapImg1];
[_imageTwo addGestureRecognizer:singleTapImg2];
[_imageThree addGestureRecognizer:singleTapImg3];
Now, you can use below code to get, which imageview clicked and perform action according to that:
-(void)singleTapping:(UIGestureRecognizer *)recognizer {
if (recognizer.view == _imageOne){
//_imageOne tapped
}
else if (recognizer.view == _imageTwo) {
//_imageTwo tapped
}else{
//_imageThree tapped
}
}
So I have an UIImageView inside of a UIScrollView, I need to capture both:
- single tap gesture
- double tap gesture
for the UIImageView
The double tap gesture is supposed to be sent to the UIScrollView as an action of zooming
The single tap gesture is captured by a UITapGestureRecognizer that I created myself.
The double tap gesture has higher priority than the single tap (when there is a double tap, zoom in the scrollview, else perform the single tap)
So far if I add the single tap gesture recognizer to the scrollview, the single tap is immediately recognized and no double tap can be recognized.
If I add the single tap gesture recognizer to the imageview, it never receives any action, but double tap works
Any suggestion is appreciated, thanks..
try this code,
Objective - C
UITapGestureRecognizer *singletap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(singleTap:)];
singletap.numberOfTapsRequired = 1;
singletap.numberOfTouchesRequired = 1;
[scrollView addGestureRecognizer:singletap];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(doubleTap:)];
doubleTap.numberOfTapsRequired =2 ;
doubleTap.numberOfTouchesRequired = 1;
[scrollView addGestureRecognizer:doubleTap];
[singleTap requireGestureRecognizerToFail:doubleTap];
Swift 3.0
var singletap = UITapGestureRecognizer(target: self, action: #selector(self.singleTap))
singletap.numberOfTapsRequired = 1
singletap.numberOfTouchesRequired = 1
scrollView.addGestureRecognizer(singletap)
var doubleTap = UITapGestureRecognizer(target: self, action: #selector(self.doubleTap))
doubleTap.numberOfTapsRequired = 2
doubleTap.numberOfTouchesRequired = 1
scrollView.addGestureRecognizer(doubleTap)
singleTap.require(toFail: doubleTap)
UITapGestureRecognizer *tapRecognizerForSingleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapAction:)];
[tapRecognizerForSingleTap setNumberOfTapsRequired:1];
UITapGestureRecognizer *tapRecognizerForDoubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doubleTapAction:)];
[tapRecognizerForDoubleTap setNumberOfTapsRequired:2];
Both are added in a same image view and to achieve what you want just add the below line.
[tapRecognizerForSingleTap requireGestureRecognizerToFail:tapRecognizerForDoubleTap];
By the above line you are asking for single tap will be only recognise if it's not double tap.
I have a UILabel and when i double tap on it ,it should assign value of Label to the other view's UITextView otherwise next view's textview should be empty.
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[holderView addGestureRecognizer:tapRecognizer];
tapRecognizer = nil;
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(LaunchText)];
[doubleTapRecognizer setNumberOfTapsRequired:2];
[doubleTapRecognizer setDelegate:self];
[holderView addGestureRecognizer:doubleTapRecognizer];
holderView.userInteractionEnabled=YES;
[tapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer];
/*n=[SingleTon getInstance];
n.Name=textLabel.text;*/
if (doubleTapRecognizer.numberOfTapsRequired==2) {
n.Name=#"hi";
}
/* if (doubleTapRecognizer.numberOfTapsRequired==2) {
n.Name=textLabel.text;
}*/
else{
//n.Name=textLabel.text;
n.Name=#"hellow";
}
-(void) LaunchText
{
[self performSegueWithIdentifier:#"textAdd" sender:self];
}
I am always getting if condition, else part never runs, What is the issue with my code?
Please try below code.
//first add single tap
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleT:)];
[tapRecognizer setNumberOfTapsRequired:1];
[holderView addGestureRecognizer:tapRecognizer];
//now add double tap
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doubleT:)];
[doubleTapRecognizer setNumberOfTapsRequired:2];
[holderView addGestureRecognizer:doubleTapRecognizer];
holderView.userInteractionEnabled=YES;
[tapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer];
Now Implement Single Tap and Double Tap Method
-(void)singleT:(UITapGestureRecognizer *)gest
{
NSLog(#"Single");
}
-(void)doubleT:(UITapGestureRecognizer *)gest
{
NSLog(#"double");
}
Maybe this will help you.
doubleTapRecognizer.numberOfTapsRequired==2 is the value you set to how many times the user has to tap to set off the gesture recognizer. it you want to allow for single and double taps you can keep a count on the gesture recognizer target and see how many times they've tapped over a giving time, like 0.2 secs or whatever time you want. I'm not 100% sure but you could also try to add two gestures to the label one for numberOfTapsRequired = 1 and numberOfTapsRequired = 2 and have them both perform different functions. Again i'm not sure on the two recognizers but you could give it a try and try the other method otherwise.
I am trying to use this code for a tap gesture recognizer, it works fine when number of taps required is set to 2, but when I set the number of taps required to 1 it stop functioning. I appreciate any help in getting this to work.
UITapGestureRecognizer *doubleTap =
[[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(tapDetected:)];
[doubleTap setNumberOfTapsRequired : 1];
[doubleTap setDelaysTouchesBegan : YES];
[self.view addGestureRecognizer:doubleTap];
You try this:-
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget: self action:#selector(tapDetected:)];
singleTap.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:singleTap];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget: self action:#selector(tapDetected:)];
doubleTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleTap];
[singleTap requireGestureRecognizerToFail:doubleTap];
This works for me.
If You only want the receiver to respond to one/first touch then setDelaysTouchesBegan:NO Since this is used for processing touches in UITouchPhaseBegan so it is analyzed and is prevented from being delivered. Property discussion from documentation:
When the value of this property is NO (the default), views analyze touch events in UITouchPhaseBegan and UITouchPhaseMoved in parallel with the receiver. When the value of the property is YES, the window suspends delivery of touch objects in the UITouchPhaseBegan phase to the view. If the gesture recognizer subsequently recognizes its gesture, these touch objects are discarded. If the gesture recognizer, however, does not recognize its gesture, the window delivers these objects to the view in a touchesBegan:withEvent: message (and possibly a follow-up touchesMoved:withEvent: message to inform it of the touches’ current locations). Set this property to YES to prevent views from processing any touches in the UITouchPhaseBegan phase that may be recognized as part of this gesture.
It looks to me like you are simply failing to set the delegate of your GestureRecognizer. Are you sure the double tap was firing successfully? When I placed your code into my project I see the same behavior but setting the delegate properly and using shouldRecognizeSimultaneouslyWithGestureRecognizer causes it to recognize the single tap properly.
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(tapDetected:)];
[doubleTap setDelegate:self];
[doubleTap setNumberOfTapsRequired : 1];
[doubleTap setDelaysTouchesBegan : YES];
[picker addGestureRecognizer:doubleTap];
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if ([gestureRecognizer isKindOfClass:UITapGestureRecognizer.class] &&
[otherGestureRecognizer isKindOfClass:UITapGestureRecognizer.class])
{
return YES;
}
else
{
return NO;
}
}
I've two UITapGestureRecognizer: singleTap and doubleTap initialized with two different actions.
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleTap:)];
[singleTap requireGestureRecognizerToFail:doubleTap];
[doubleTap setNumberOfTapsRequired:2];
[imageView addGestureRecognizer:doubleTap];
[imageView addGestureRecognizer:singleTap];
When I run my app in simulator the single tap responds correctly but not the double tap ! When I double clicks nothings happens, I suppose iOS dose recognize the double tap because the action of single tap doesn't being called (due to [singleTap requireGestureRecognizerToFail:doubleTap];), but I can't understand why doesn't it do the action handleDoubleTap.
I think the problem is that UIImageView and UILabel both override the default value of YES for the userInteractionEnabled property, and sets it to NO.
Add imageView.userInteractionEnabled = YES; and try again.
The following code works for me:
- (void)handleTap:(UIGestureRecognizer*)gr {
NSLog(#"----------------- tap ----------------");
}
- (void)handleDoubleTap:(UIGestureRecognizer*)gr {
NSLog(#"================= double tap ============");
}
- (XXXView*)createXXXView {
XXXView *view = [[[XXXView alloc] init] autorelease];
view.xxx=...;//irrelevant
UITapGestureRecognizer *dtr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleTap:)];
dtr.numberOfTapsRequired = 2;
UIGestureRecognizer *tr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
[tr requireGestureRecognizerToFail:dtr];
[view addGestureRecognizer:tr];
[view addGestureRecognizer:dtr];
return view;
}