1

I have a following view hierarchy in my app:

-- UIScrollView (canCancelContentTouches is NO)
   -- UIView #1 (UISwipeGestureRecognizer is bound to it to track horizontal swipes)
      -- UIView #2 (touchesBegan/touchesMoved/touchesEnded are implemented here to allow
                    dragging this view inside its parent; implementation is very
                    straightforward and I'm NOT calling supermethods here).

When I start dragging view #2, it sometimes triggers the swipe gesture recognizer. I can't really see the pattern, but this happens quite often.

Is there any way to suppress the touches processing while dragging?

Sergey Mikhanov
  • 8,880
  • 9
  • 44
  • 54

1 Answers1

2

Use a UIPanGestureRecognizer on view #2 instead of handling the touches directly. In the delegate for both recognizers, return NO from gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:.

OR

In the UISwipeGestureRecognizer's delegate, implement this:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return gestureRecognizer.view == touch.view;
}
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • This will definitely work, but those views are not intended to know about each other (i.e. are dealing with completely different subsets of functionality) – Sergey Mikhanov Nov 14 '11 at 23:02
  • I got my view hierarchy wrong in the original question, it's updated now. Also, I have tried your suggestions and got a different behavior. Now with any of the options you outlined, view #2 is very hard to pick: 9 of 10 drag attempts are delivered to swipe recognizer, not to one for pan. As soon as I'm able to pick that view and starting to drag it, swipe is not triggered any more (so the initial problem is gone). I can see that in `gestureRecognizer:shouldReceiveTouch:` the `touch.view` almost always contain #1 -- even is I'm carefully dragging from the center of #2. Any idea why? – Sergey Mikhanov Nov 15 '11 at 01:02
  • OK, so after returning #2 in #1's `hitTest:` properly, I got this solved. Had to made #1 dependent on #1 though. I'll accept your answer as it served as a very useful guidance. Thanks! – Sergey Mikhanov Nov 15 '11 at 01:27