I want to use tap gesture recognizer for detecting the user taps .i don't have much knowledge about this .Can anyone please help me for this .Tap Gesture .
I want to set the maximum count and i want to detect the user taps count if it is equal i want to perform some operation .Please help me to do this .
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 1;
[self.imageforcapture addGestureRecognizer:tapGesture];
The above code is simple example of tea gesture recognizer .
Thanks in advance !!!!
Init Method :
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 1;
[self.imageforcapture addGestureRecognizer:tapGesture];
Handle Method :
- (void)handleTapGesture:(UITapGestureRecognizer*)sender {
i=i+1;
if(i==10)
{
//Prforme task here
}
}
Declare i as global variable
Use this:
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(foundTap:)];
tapRecognizer.numberOfTapsRequired = 1;
tapRecognizer.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:tapRecognizer];
-(void)foundTap:(UITapGestureRecognizer *)recognizer
{
// Your code goes here
}
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 1;
self.imageforcapture.userInteractionEnabled = YES;
[self.imageforcapture addGestureRecognizer:tapGesture];
First, you need to allow imageView to receive touches by adding line above.
Define global variable i.
#property NSInteger i;
in viewDidLoad set the value of i to 0
self.i = 0;
After that you need to handle that global i variable that will track taps.
- (void)handleTapGesture:(UITapGestureRecognizer*)sender {
self.i++;
if (self.i == 10) {
//Perform your expected behaviour
}
}
Related
I am trying to run a method by 2 times tapping on tv remote, consider tapping not clicking, but the touch surface does't recognize the taps. Instead, clicking two times runs the doubleTapping method.
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
doubleTap.allowedTouchTypes =#[[NSNumber numberWithInteger:UITouchTypeIndirect]];
doubleTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleTap];
- (void)handleTap:(UITapGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateBegan)
{
// handling code
NSLog(#"2 times");
}
}
I am missing something?
I was forgot to mention the UIPressType value , now due to position of remote's surface (Up / Down / Right / Left) you can now detect user's tap direction and add numberOfTapsRequired to the action :
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
[tapGestureRecognizer setAllowedPressTypes:#[#(UIPressTypeLeftArrow)]];
[tapGestureRecognizer setNumberOfTapsRequired:2];
[self.view addGestureRecognizer:tapGestureRecognizer];
How can you detect the touch of the navigationBar title in iOS 7. The code below worked great prior to iOS 7 but no longer is the touch detected.
UITapGestureRecognizer* tapRecon = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(navigationBarTap:)];
tapRecon.numberOfTapsRequired = 1;
[[self.navigationController.navigationBar.subviews objectAtIndex:1] setUserInteractionEnabled:YES];
[[self.navigationController.navigationBar.subviews objectAtIndex:1] addGestureRecognizer:tapRecon];
thanks,
Greg
Can you print the description of self.navigationController.navigationBar.subviews objectAtIndex:1]? (Just NSLog it).
The same code works good for me:
UIView *viewWithTitleLabel = self.navigationController.navigationBar.subviews[1];
viewWithTitleLabel.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGestureOccured:)];
[viewWithTitleLabel addGestureRecognizer:tapGesture];
When do you call your code? Seems to be that "subviews[1]" - is not your desired view.
TapGestureRecognizer* tapRecon = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(navigationBarTap:)];
tapRecon.numberOfTapsRequired = 1;
NSInteger objIndex = 1;
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.1f) objIndex = 2;
[[self.navigationController.navigationBar.subviews objectAtIndex:objIndex] setUserInteractionEnabled:YES];
[[self.navigationController.navigationBar.subviews objectAtIndex:objIndex] addGestureRecognizer:tapRecon];
In iOS 8 (and probably 7), the title object has the private class UINavigationItemView that you can check for and then apply your gesture recognition code more judiciously, rather than blindly using array indices.
for (UIView *view in self.navigationController.navigationBar.subviews)
{
if ([view isKindOfClass:NSClassFromString(#"UINavigationItemView")] &&
![view isKindOfClass:NSClassFromString(#"UINavigationItemButtonView")])
{
view.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(selectorGoesHere:)];
[view addGestureRecognizer:tapGesture];
break;
}
}
Whether or not this will pass app review is another question – one that I couldn't answer.
I have a button and I'm testing the taps on it, with one tap it change a background color, with two taps another color and with three taps another color again.
The code is:
- (IBAction) button
{
UITapGestureRecognizer *tapOnce = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapOnce:)];
UITapGestureRecognizer *tapTwice = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapTwice:)];
UITapGestureRecognizer *tapTrice = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapTrice:)];
tapOnce.numberOfTapsRequired = 1;
tapTwice.numberOfTapsRequired = 2;
tapTrice.numberOfTapsRequired = 3;
//stops tapOnce from overriding tapTwice
[tapOnce requireGestureRecognizerToFail:tapTwice];
[tapTwice requireGestureRecognizerToFail:tapTrice];
//then need to add the gesture recogniser to a view - this will be the view that recognises the gesture
[self.view addGestureRecognizer:tapOnce];
[self.view addGestureRecognizer:tapTwice];
[self.view addGestureRecognizer:tapTrice];
}
- (void)tapOnce:(UIGestureRecognizer *)gesture
{
self.view.backgroundColor = [UIColor redColor];
}
- (void)tapTwice:(UIGestureRecognizer *)gesture
{
self.view.backgroundColor = [UIColor blackColor];
}
- (void)tapTrice:(UIGestureRecognizer *)gesture
{
self.view.backgroundColor = [UIColor yellowColor];
}
The problem is that the first tap don't works, the other yes.
If I use this code without button it works perfectly.
Thanks.
If you want the colors to change on tap of button, you should add these gestures on button in viewDidLoad method or so rather than on the same button action. The above code will repeatedly add gestures on tap of the button to the self.view and not on button.
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tapOnce = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapOnce:)];
UITapGestureRecognizer *tapTwice = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapTwice:)];
UITapGestureRecognizer *tapTrice = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapTrice:)];
tapOnce.numberOfTapsRequired = 1;
tapTwice.numberOfTapsRequired = 2;
tapTrice.numberOfTapsRequired = 3;
//stops tapOnce from overriding tapTwice
[tapOnce requireGestureRecognizerToFail:tapTwice];
[tapTwice requireGestureRecognizerToFail:tapTrice];
//then need to add the gesture recogniser to a view - this will be the view that recognises the gesture
[self.button addGestureRecognizer:tapOnce]; //remove the other button action which calls method `button`
[self.button addGestureRecognizer:tapTwice];
[self.button addGestureRecognizer:tapTrice];
}
I am using the code below
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(processTap)];
- (void) processTap
{
//do something
}
But I need to send a data to processTap function. Is there anyway to do this?
Example:
UIImageView *myPhoto = [[UIImageView alloc] initWithImage: [UIImage imageNamed:#"tab-me-plese.png"]];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(processTap:)];
[myPhoto setTag:1]; //set tag value
[myPhoto addGestureRecognizer:tap];
[myPhoto setUserInteractionEnabled:YES];
- (void)processTap:(UIGestureRecognizer *)sender
{
NSLog(#"tabbed!!");
NSLog(#"%d", sender.view.tag);
}
There's a good answer to a similar question at iOS - UITapGestureRecognizer - Selector with Arguments, but if you want to pass data that you don't want to attach to the view, I recommend creating a second function that has access to the data you need. For example:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(passDataToProcessTap)];
- (void)passDataToProcessTap {
[self processTapWithArgument:self.infoToPass];
// Another option is to use a static variable,
// Or if it's not dynamic data, you can just hard code it
}
- (void) processTapWithArgument:(id)argument {
//do something
}
Im working on an app with zooming function.
In this app I have this button. I want it to respond to tapping in several ways:
Single tap: Zoom in slightly.
Double tap: Zoom in to the max.
Ive tried several options to achieve this but none are what I want.
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:tapGesture];
[zoomin addTarget:self action:#selector(zoominMax) forControlEvents:UIControlEventTouchDownRepeat];
Both work on single and double tap but when I press the button once to slightly zoom and seconds later I press it again it doesn't zoom in slightly, it zooms in to the max.
It is possible to fix this with a timer and location check so that when u tap and tap again u can be sure that the location is in a similar area and the taps happened within timer range.
But is this what I really need?
Is there a simpler solution?
the solution provided by omz is not good.
where as you can do this by simply adding these lines of code like posted here. Double-tap or two single-taps?
NOTE THE MAGICAL LINE : [tapRecg requireGestureRecognizerToFail:doubleTapRecg];
ABSTRACT:
UITapGestureRecognizer *doubleTapRecg = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(doubleTapped:)];
doubleTapRecg.delegate = self;
doubleTapRecg.numberOfTapsRequired = 2;
doubleTapRecg.numberOfTouchesRequired = 1;
[view addGestureRecognizer:doubleTapRecg];
UITapGestureRecognizer *tapRecg = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(tapped:)];
tapRecg.delegate = self;
tapRecg.numberOfTapsRequired = 1;
tapRecg.numberOfTouchesRequired = 1;
[view addGestureRecognizer:tapRecg];
[tapRecg requireGestureRecognizerToFail:doubleTapRecg];
[doubleTapRecg release];
[tapRecg release];
You can do it with two gesture recognizers and a timer:
UITapGestureRecognizer *tapGestureRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap:)] autorelease];
[myView addGestureRecognizer:tapGestureRecognizer];
UITapGestureRecognizer *doubleTapGestureRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doubleTap:)] autorelease];
doubleTapGestureRecognizer.numberOfTapsRequired = 2;
[myView addGestureRecognizer:doubleTapGestureRecognizer];
You'll have to use a slight delay in your tap: action before zooming in slightly because the first tap could be followed by a second tap:
- (void)tap:(UITapGestureRecognizer *)recognizer
{
[self performSelector:#selector(singleTap) withObject:nil afterDelay:0.25];
}
- (void)singleTap
{
//slightly zoom in...
}
- (void)doubleTap:(UITapGestureRecognizer *)recognizer
{
//Cancel the timer for the single tap action:
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(singleTap) object:nil];
//zoom in to the max zoom level...
}