5

This is what I'm trying to do:

I want to start recognizing a gesture in a subview and then, when the finger leaves the subview, let the superview take over and handle the gesture. Is that possible?

enter image description here

At the moment, the view in which the gesture started "wins". Is there any way to "hand over" a gesture from one view to another "on the fly", without lifting the finger off the screen?

This is the code for a simple demo:

@implementation SPWKViewController {
    UIView *_innerView;
    UIPanGestureRecognizer *_outerGestureRecognizer;
    UIPanGestureRecognizer *_innerGestureRecognizer;

    UILabel *_label;
}

- (void)loadView
{
    [super loadView];

    self.view.backgroundColor = [UIColor yellowColor];

    _innerView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, self.view.frame.size.width - 100, 200)];
    _innerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    _innerView.backgroundColor = [UIColor greenColor];

    _label = [[UILabel alloc] initWithFrame:CGRectMake(0, 300, self.view.frame.size.width, 40)];
    _label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    _label.textAlignment = NSTextAlignmentCenter;
    _label.backgroundColor = [UIColor clearColor];

    [self.view addSubview:_innerView];
    [self.view addSubview:_label];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    _outerGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
    [self.view addGestureRecognizer:_outerGestureRecognizer];

    _innerGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
    [_innerView addGestureRecognizer:_innerGestureRecognizer];
}

- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state == UIGestureRecognizerStateChanged) {
        if (gestureRecognizer == _outerGestureRecognizer) {
            _label.text = @"outer";
        } else {
            _label.text = @"inner";
        }
    } else {
        _label.text = @"";
    }
}

@end

Some background information: The actual problem I'd like to solve is this: I'd like to put a UIWebView inside of a UIScrollView. When I scroll to the bottom of the UIWebView, I want the parent UIScrollView to "take over" and keep scrolling without having to lift the finger off the screen first. I figured that in order to achieve that, I'd need to "hand over" the gesture from the UIWebView to the parent UIScrollView.

Thank you!

Johannes Fahrenkrug
  • 42,912
  • 19
  • 126
  • 165
  • I know that you can just put a UIScrollView inside a UIScrollView and this behaviour "Just Works". I take it you've determined this is not the case for a UIWebView? – hatfinch Nov 08 '13 at 11:41
  • 1
    I had a quite similar question (http://stackoverflow.com/q/21554327/235297) and came to the conclusion that it's not possible. But I also thought that `UIWebView`, containing a `UIScrollView` would automatically "trigger" the outer scroll view as soon as it hits the edge, so that the outer scroll view would scroll as well. Doesn't it do that? – Ortwin Gentz Feb 24 '14 at 16:10

0 Answers0