How to add UIPanGestureRecognizer to UIScrollView subviews

Shubham picture Shubham · Feb 4, 2012 · Viewed 16.2k times · Source

I am adding subviews to a UIScrollView and then I add UIPanGestureRecognizer to these subviews. Everything works fine but now after adding UIPanGestureRecognizer to subviews of the scroll view, scrolling is not possible.

What can be the possible solution to this issue?

Answer

wattson12 picture wattson12 · Feb 4, 2012

The problem is that the pan gesture recognizer is what is used in the scroll view to control scrolling. Your gesture recogniser is taking priority and disabling the scroll views

If you want to always be able to scroll you can set your gesture recogniser to require the scroll views one to fail before it will work:

[self.myCustomPanRecognizer requireGestureRecognizerToFail:self.scrollView.panGestureRecognizer]; 

Edit: as Bastian pointed out in the comments, the reference to pan guesture is only in iOS 5, prior to this, check the array of gesture recognisers and find the one of type UIPanGestureRecognizer

if you want both to work you may need to do something to separate your recogniser from the scroll views, e.g. make the user tap and hold before your custom recogniser will be recognised.

there is also a delegate method which will allow both recognisers to work together, but I'm not sure how well this works when both are the same type