UIScrollView overrides my subview's pan gesture recognizers

12,011

Solution 1

Here's what works for me:

UIPanGestureRecognizer *subviewPanRecognizer = [[UIPanGestureRecognizer alloc]
    initWithTarget:self action:@selector(panSubview:)];
[subview addGestureRecognizer:subviewPanRecognizer];

// play nice with subview's pan gesture
[scrollView.panGestureRecognizer 
    requireGestureRecognizerToFail:subviewPanRecognizer];

Solution 2

Set canCancelContentTouches property of UIScrollView to false if you don't want to scroll on touching subviews.

Original answer

Share:
12,011
Mr Ordinary
Author by

Mr Ordinary

I am learning for pleasure and profit.

Updated on June 04, 2022

Comments

  • Mr Ordinary
    Mr Ordinary almost 2 years

    If I have a scrollView with a subview and the subview has a pan gesture recognizer, the scrollView's pan gesture override's the subview's pan. What I want is the opposite, I think, so that is I drag a subview it will pan within the scroll view, yet if I touch another area the scroll view will pan as normal. Is there an easy way to set that up?

  • Norswap
    Norswap over 10 years
    That seems to be the default behaviour (that's what I'm observing in practice in a UIScrollView with a UIImageView subview). Could someone confirm/infirm?
  • Anthony Mills
    Anthony Mills almost 9 years
    Works beautifully, especially when applying a delegate on the subview and implementing gestureRecognizerShouldBegin:.