Uitapgesturerecognizer - Single Tap and Double Tap

UITapGestureRecognizer - single tap and double tap

UITapGestureRecognizer *singleTap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doSingleTap)] autorelease];
singleTap.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:singleTap];

UITapGestureRecognizer *doubleTap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doDoubleTap)] autorelease];
doubleTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleTap];

[singleTap requireGestureRecognizerToFail:doubleTap];

Note: If you are using numberOfTouchesRequired it has to be .numberOfTouchesRequired = 1;

For Swift

let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(didPressPartButton))
singleTapGesture.numberOfTapsRequired = 1
view.addGestureRecognizer(singleTapGesture)

let doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(didDoubleTap))
doubleTapGesture.numberOfTapsRequired = 2
view.addGestureRecognizer(doubleTapGesture)

singleTapGesture.require(toFail: doubleTapGesture)

UITapGestureRecognizer doubletap still triggers singletap action

You can achieve it like this as well ... add delay to your single tap to check if its double tap.. if double tap then cancel performance of single tap .. simple methodology

var tapCount = 0
func tapGestureRecognizer() {
tapCount += 1
switch (tapCount)
{
case 1: //single tap
self.perform(#selector(singleTap), with: nil, afterDelay: 0.2)
case 2: //double tap


NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(singleTap), object: nil)

doubleTap()
break;
default:
break;
}
if (tapCount>2){
tapCount = 0
}
}


@objc func singleTap(){

}

@objc func doubleTap() {

}

How to filter a quick multiple tap for single Tap event and double tap event

You can use the gesture recognizer's delegate to temporarily disable both gestures after a double click. The code below disables the gestures for 0.6 seconds after a double click.

You'll need a couple properties

@property int disableGestures;
@property CFTimeInterval timeStamp;

and some additional initialization

self.disableGestures = NO;
singleTap.delegate = self;
doubleClick.delegate = self;

and you'll need to conform to the protocol, and implement the shouldReceiveTouch method

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ( self.disableGestures )
{
if ( CACurrentMediaTime() - self.timeStamp < 0.6 )
return NO;
self.disableGestures = NO;
}
return YES;
}

All that's left is to disable the gestures after a double click

- (void)doubleClickDetected:(UITapGestureRecognizer *)gesture
{
self.disableGestures = YES;
self.timeStamp = CACurrentMediaTime();

// normal processing for the double click goes here
}

Allow both single tap gesture recognizer and double tap in UIScrollView

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)

How to accelerate the identification of a single tap over a double tap?

I found the answer on this link

The swift version:

class UIShortTapGestureRecognizer: UITapGestureRecognizer {
let tapMaxDelay: Double = 0.3

override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
super.touchesBegan(touches, withEvent: event)
delay(tapMaxDelay) {
// Enough time has passed and the gesture was not recognized -> It has failed.
if self.state != UIGestureRecognizerState.Ended {
self.state = UIGestureRecognizerState.Failed
}
}
}
}

With delay(delay: Double, closure:()->()):

class func delay(delay:Double, closure:()->()) {
dispatch_after(dispatch_time( DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), closure)
}

iOS distinguish single tap and double tap at first opening

Write the below lines in ViewDidLoad

[singleTap requireGestureRecognizerToFail:doubleTap];

Swift Double Tap Recognizer Responding To A Single Tap Recognizer

This may be because of some mismanagement the tap gesture is not able to add for that recognizer. You may not need to send the gesture to the selector function.

Change this

let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(labelClicked(gesture:)))

To

let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(labelClicked))

in addDoubleTabGestureRecognizerToWordLabel()

and remove the receiving parameter in labelClicked().

Hope this helps!

How to detect tap and double tap at same time using UITapGestureRecognizer?

Here is what I have used in one of my old projects, I hope it helps you out man.

UITapGestureRecognizer *singleTap = [[[UITapGestureRecognizer alloc] initWithTarget:    self action:@selector(doSingleTap)] autorelease];
singleTap.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:singleTap];

UITapGestureRecognizer *doubleTap = [[[UITapGestureRecognizer alloc] initWithTarget: self action:@selector(doDoubleTap)] autorelease];
doubleTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleTap];

[singleTap requireGestureRecognizerToFail:doubleTap];

UIGestureRecognizers for single and double tap set in the xib

It's a very good question. If you add gestures to code like this

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget: self action:@selector(singleTap)];

singleTap.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:singleTap];

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget: self action:@selector(doubleTap)] ;
doubleTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleTap];

[singleTap requireGestureRecognizerToFail:doubleTap];

And all works fine because you canceled first gesture here

[singleTap requireGestureRecognizerToFail:doubleTap];

If you add two gestures in xib you always should cancel single tap if there was a double tap. And you always need to use 2 properties for gestures and use

[self.firstGestureProperty requireGestureRecognizerToFail:self.secondGestureOroperty];

For single tap:

Sample Image

For double tap:

Sample Image

Source code:

Sample Image

And everything works fine.



Related Topics



Leave a reply



Submit