Drag Gesture Recognizer interfere with swipe Gesture Recognizer - ios

I have a problem about gesture recognizer. I have two gesture recognizers in my view, one added to the background superview to swipe to change background color, the other one is added to a subview which could be dragged around.
The problem is the drag gesture is interfered with the swipe. When I drag the view around, in some case, drag operation will be recognized as a swipe and trigger the swipe operation. I don't want this, I just want swipe could be recognized after finish this drag operation. This situation is more often when I drag the subview around quickly. Every time during the drag, the swipe operation will be triggered.

You need to setup your lesser UIGestureRecognizer with requireGestureRecognizerToFail: and pass your recognizer that you want to be important to it.
UIView *subview = ....
[self addSubview:subview];
[self.gestureRecognizer requireGestureRecognizerToFail:subview.gestureRecognizer];

Related

How to make a UIScrollView, some UIButtons and UIPanGestureRecognizer work together?

I got a scroll view with a bunch of buttons, which I want to both touch and drag.
To choose if it's a touch or a drag (not drag of the scroll view, "physically" dragging the button), I check the pan gesture recognizer of the button to see how far away the drag would be, if it's over a threshold I snap the button into drag mode and, removing it from the scroll view and adding it to my view controller's view (at the same location, so looks like it's in the same place).
This all works kinda smooth, except when I want to scroll and the pan fires at the same time. I need a way for the pan gesture recognizer to choose, based on velocity, if the scroll view should scroll or the button should be dragged.
The question: Is there a way for a UIPanGestureRecognizer to tell a UIScrollView to continue scrolling and cancel itself?

Cannot swipe to switch UICollectionView page if you begin gesture on a cell

I have a UICollectionView that contains a single UIButton in each cell. I've discovered it's difficult to swipe between the horizontal pages because if you touch down on a cell to begin the swipe, the UIButton touch events are triggered instead of allowing the swipe gesture to occur. I do have Delays Content Touches enabled for the collection view. What can I do to solve this so that it will recognize the page pan swipe gesture when you begin swiping on a cell? Perhaps the amount of delay can be increased before it recognizes a UIControl event?
Note that I do need to preserve the touch events for the buttons - I need to know when these events occur: TouchDown, TouchDragEnter, TouchCancel, TouchDragExit, and TouchUpInside.
Without the knowledge of whether you add the gesture recognizer in storyboard or by code, I can only suggest that you can try to creates a dependency relationship between the cell's gesture recognizer and the other gesture recognizer.
- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer
You have to set delaysContentTouches to YES on your UICollectionView. That way, there will be a small delay before the cells will receive a touch, and the built-in swipe recogniser can do its work.
You can also do that from the nib/storyboard by ticking the appropriate checkmark in IB:
It seems this was only a problem in iOS 8.0 (at least the simulator), as I'm no longer experiencing it in iOS 8.1.

How to detect simultaneous swipe and touch-ended

Is it possible to detect direction of swipe and simultaneously detect release of finger from screen? When I use gesture recognizer from Object library, it works only swipe or only touchesEnded, but not simultaneously.
Set cancelsTouchesInView to NO on your swipe gesture recognizer. If you're working from interface builder, it's the Canceled in View checkbox.

UIPanGestureRecognizer requireGestureRecognizerToFail delays UIScrollView

i have a UIPanGestureRecognizer on my rootViewControllers view
that controls only 2 finger swipe ( min and max are set on 2 )
I have a couple of UITableView and UIScrollView on my rootViewControllers view.
But the 2 finger swipe should always have number 1 priority
so i put a requireGestureRecognizerToFail on my UITableView and UIScrollView's panGesture property.
this works perfectly but now when i pan my UITableView and UIScrollView, it doesn't move until i stop swiping.
Is there a solution for this?
i have added a sample project to display what the problem is: https://github.com/avalanched/UIScrollViewTest
You will need to allow the gesture recognizers to simultaneously recognize by setting the delegate on all the gesture recognizers and implementing gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:.
See Apple's UIGestureRecognizerDelegate Documentation.
You also need to remove the requireGestureRecognizerToFail calls, these are what causes the delay.

Cancel UIGestureRecognizer if subview contains gesture?

I was originally using the UITouch methods (touchesMoved: withEvent:) in order to detect the movement in a view. Due to the fact that it seemed not to update often enough I switched to using a UIPanGestureRecognizer (though this didn't actually fix the initial problem).
After using this the touches received for movements responded no matter what it was touching (different subviews) which I actually prefer. However now I have an issue with all the subviews which have movement. I need the gesture recognizer to still move the view even while touching subviews, however if the subview also moves in this direction (left/right) I need my recognizer to cancel.
It seems like gestureRecognizerShouldBegin: might be somewhere to start, but in my case, I cannot account for all subviews.
Is it possible to make pan gesture recognizer cancel if the subview (i.e. another pan recognizer, UISlider, horizontal scrollview, etc.) touched needs to be moved in left/right directions without having to account for each subview individually?

Resources