Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prioritize gesture recognizers and touches in a UIView

From reading the UIGestureRecognizer Class Reference it is implied that the API will handle the prioritizing of touches and gesture controls for you, making sure that your touchesBegan and related methods are not called on the view unless the gesture recognizers have first failed:

A window delivers touch events to a gesture recognizer before it delivers them to the hit-tested view attached to the gesture recognizer. Generally, if a gesture recognizer analyzes the stream of touches in a multi-touch sequence and does not recognize its gesture, the view receives the full complement of touches. If a gesture recognizer recognizes its gesture, the remaining touches for the view are cancelled.

I have added a swipe gesture to my view, and it is working. Via some logging, when I do a single swipe, the method reports as such. However, my touchesBegan method is also reporting via its log, despite that the touchesCancelled method is, as expected, also receiving a message.

I want, and expect, the gesture recognize to prevent touchesBegan or touchesMoved from being called.

So my question is: for the gesture recognizer to in fact delay touches based on its state, is there additional setup necessary? The docs do not suggest anything else as necessary.

My setup is simply:

 swipeUpTwoFinger=[[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(doubleSwipeUp:)]autorelease];
swipeUpTwoFinger.direction=UISwipeGestureRecognizerDirectionUp;
swipeUpTwoFinger.numberOfTouchesRequired=2;
[self addGestureRecognizer:swipeUpTwoFinger];

I have also tried this test to make sure a recognizer has failed before processing with touchesBegan (this test should not be necessary if you believe what the docs say above) but the touchesBegan is still processing the log line after this test:

if (swipeUpTwoFinger.state==UIGestureRecognizerStateFailed)

like image 544
johnbakers Avatar asked Apr 08 '13 01:04

johnbakers


People also ask

Which method handles touch event in IOS?

There are two ways to handle touch events yourself. Use gesture recognizers to track the touches; see Handling UIKit gestures. Track the touches directly in your UIView subclass; see Handling touches in your view.

How do you use pan gestures?

A pan gesture occurs any time a person moves one or more fingers around the screen. A screen-edge pan gesture is a specialized pan gesture that originates from the edge of the screen. Use the UIPanGestureRecognizer class for pan gestures and the UIScreenEdgePanGestureRecognizer class for screen-edge pan gestures.

How do I use UISwipeGestureRecognizer?

You can select right, left, up or down. One by one, select the swipe gesture recognizer, control + drag to your view controller. Insert the name (let us say leftGesture, rightGesture, upGesture and downGesture), change the connection to: Action and type to: UISwipeGestureRecognizer.

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.


1 Answers

It sounds like you need: swipeUpTwoFinger.delaysTouchesBegan = YES;

like image 142
user352891 Avatar answered Sep 28 '22 02:09

user352891