Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Give swipe gesture priority over button

I have two UIButtons in a view (one is YES, the other NO). I now want to add a "did not answer", which would be indicated by a downward swipe on the view.

The problem is that the user may swipe down on the view, but in the process hit one of the buttons. When this happens I want to ignore the button press if there was a swipe underway. If it is just a tap on a button, the answer is recorded.

So, if the swipe occurs, I want to call the swipe gesture's action method. If it is determined no swipe occurred but one of the two buttons was touched I want to call their respective action methods. But if a button was touched in the process of a swipe, I want to call only the swipe gesture's action method.

I know there is a way but I'm wondering whether there is an EASY way of doing this. TIA for suggestions.

like image 925
RegularExpression Avatar asked Oct 24 '25 00:10

RegularExpression


2 Answers

Since UISwipeGestureRecognizer is a "discrete" gesture, it just triggers a single action when recognized and it won't allow you to detect the end of the gesture.

So to prevent other touches during the gesture, I'd recommend using a UIPanGestureRecognizer instead since it can track your gesture from beginning to end. Then you can try setting your gesture's cancelsTouchesInView property to YES to cancel all other touches in the view that happen while that pan gesture is recognized, ex:

gesture.cancelsTouchesInView = YES;
gesture.delaysTouchesBegan = YES;
like image 57
Lyndsey Scott Avatar answered Oct 26 '25 16:10

Lyndsey Scott


Your buttons should be registering touch up in view, so that someone who taps on the button can drag off if they decide not to proceed.

For your other swipe gesture, your buttons will not register a touch up in view during a swipe gesture, even if the swipe passes over the button or ends on it.

like image 37
Duncan Babbage Avatar answered Oct 26 '25 14:10

Duncan Babbage