3

I have a UIScrollView where I added a subview. The scrollview scrolls fine vertically and that is all it should do. I would now like to recognize left/right swipes in the subview with the help of a UISwipeGestureRecognizer. I know it is possible, but I have not come across a solution and several tries have been unsuccessful.

the_critic
  • 12,720
  • 19
  • 67
  • 115

2 Answers2

5

Try these:

  • Set the delegate of your UIGestureRecognizer and

  • Implement shouldRecognizeSimultaneouslyWithGestureRecognizer:

    -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
    }
    
  • Implement shouldReceiveTouch:

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
    {
     return YES;
    }
    

Hope this helps

FD_
  • 12,947
  • 4
  • 35
  • 62
  • Nope, I have tried that before, without success :(. I think the UIScrollView just swallows every drag that is detected. – the_critic Dec 27 '12 at 12:50
  • Are you sure you implemented shouldRecognizeSimultaneouslyWithGestureRecognizer and not only shouldReceiveTouch? – FD_ Dec 27 '12 at 12:51
  • Have you checked if your delegate methods get called? – FD_ Dec 27 '12 at 12:54
  • Yes I did implement `-shouldRecognizeSimultaneouslyWithGestureRecognizer::` but strangely they do not get called... – the_critic Dec 27 '12 at 12:56
  • Are you setting the delegate correctly? Are you adding the gestureRecognizer to the UIScrollView? – FD_ Dec 27 '12 at 12:58
  • No, I am not adding the gesture recognizer to the UIScrollView because I want it only to be recognized when I do the gesture in the subview. Adding it to the scrollview would not make any sense, right ? – the_critic Dec 27 '12 at 13:01
  • I think the problem is really that I have nested the subview in many other views. Is there a way to let the touches all the way through ? – the_critic Dec 27 '12 at 13:04
  • So I have a scrollview where I add a content view and in this content view I add the subview where I would like to detect touches. So the swipe has to basically go through the content view – the_critic Dec 27 '12 at 13:19
0

I was able to accomplish something related (adding a two-finger swipe gesture recognizer directly on the scroll view) with the new iOS 7 UIGestureRecognizerDelegate method:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
        shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return otherGestureRecognizer == scrollView.panGestureRecognizer;
}

However, the results were not perfect - the delay waiting for the swipe gesture recognizer to fail first causes a lag in the scrollview's pan gesture recognizer getting its touches, so normal scrolling is noticeably delayed when you start to scroll.