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?
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!