Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone iOS how to add a UILongPressGestureRecognizer and UITapGestureRecognizer to the same control and prevent conflict?

Tags:

I'm building an iPhone app that would let the user rearrange some of the UI elements on the screen.

How can I add a tap gesture recognizer and a long press gesture recognizer to the same UIView? When I lift up the finger from the long press, the tap gesture recognizer fires. How can I temporarily disable the tap gesture recognizer or prevent it from firing when the user is performing a long press?

Thank you!

like image 984
Alex Stone Avatar asked Mar 15 '12 21:03

Alex Stone


People also ask

How do I add tap gestures in swift 5?

Adding a Tap Gesture Recognizer in Interface Builder You don't need to switch between the code editor and Interface Builder. Open Main. storyboard and drag a tap gesture recognizer from the Object Library and drop it onto the view we added earlier. The tap gesture recognizer appears in the Document Outline on the left.

How do you use UILongPressGestureRecognizer?

UILongPressGestureRecognizer is a concrete subclass of UIGestureRecognizer . The user must press one or more fingers on a view and hold them there for a minimum period of time before the action triggers.

What is gesture in Swift?

The tap gesture recognizer appears in the Document Outline on the left. We need to implement an action that defines what happens when the user taps the image view. Open ViewController. swift and define a method with name didTapImageView(_:) . It accepts the tap gesture recognizer as its only argument.


2 Answers

To allow both gestures to work together, implement the following delegate method:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{     return YES; } 

To make it so that the long press has first priority, do:

[tapGesture requireGestureRecognizerToFail:longPress];

like image 193
Snowman Avatar answered Oct 12 '22 23:10

Snowman


To combine successfully both you need:

1º Add to interface gesture delegate at header

@interface ViewController : ViewController <UIGestureRecognizerDelegate> 

2º Create gesture events and add to a view into source file:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touch:)];     [tap setNumberOfTapsRequired:1]; // Set your own number here     [tap setDelegate:self]; // Add the <UIGestureRecognizerDelegate> protocol      UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTouch:)];     [longTap setNumberOfTapsRequired:0]; // Set your own number here     [longTap setMinimumPressDuration:1.0];     [longTap setDelegate:self]; // Add the <UIGestureRecognizerDelegate> protocol     [tap requireGestureRecognizerToFail:longTap];   // Priority long      [self.view addGestureRecognizer:tap];     [self.view addGestureRecognizer:longTap]; 

3º Add callbacks in source file:

- (void) touch: (UITapGestureRecognizer *)recognizer {     CGPoint location = [recognizer locationInView: self.HUDview];     if (recognizer.state == UIGestureRecognizerStateBegan)     {         NSLog(@"touch UIGestureRecognizerStateBegan");     }     if (recognizer.state == UIGestureRecognizerStateEnded)     {         NSLog(@"touch UIGestureRecognizerStateEnded");         //NSLog(@"Position of touch: %.3f, %.3f", location.x, location.y);    // Position landscape     } }  - (void) longTouch: (UILongPressGestureRecognizer *)recognizer {     if (recognizer.state == UIGestureRecognizerStateBegan)     {         NSLog(@"longTouch UIGestureRecognizerStateBegan");     }     if (recognizer.state == UIGestureRecognizerStateEnded)     {         NSLog(@"longTouch UIGestureRecognizerStateEnded");     } } 

4º Set gesture recognizer available:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {     return YES; } 
like image 24
vgonisanz Avatar answered Oct 12 '22 23:10

vgonisanz