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!
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.
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.
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.
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];
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With