I am working on an iphone (iOS 4.0 or later) app and having some troubles with touch handling between multiple views. I am having a view structure like this
---> A superView
|
---> SubView - A
|
---> SubView - B (exactly on top of A, completely blocking A).
Basically I have a superView, and sibling subviews A and B. B has the same frame as A, hence hiding A completely.
Now my requirement is this.
- SubView B should receive all swipe and tap (single and double) gestures.
- SubView A should receive all pinch gestures.
This is how I added gesture recognizers to the views
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(leftSwipe:)];
[leftSwipe setDirection:(UISwipeGestureRecognizerDirectionLeft)];
leftSwipe.delegate = self;
[bView addGestureRecognizer:leftSwipe];
[leftSwipe release];
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(rightSwipe:)];
[rightSwipe setDirection:(UISwipeGestureRecognizerDirectionRight)];
rightSwipe.delegate = self;
[bView addGestureRecognizer:rightSwipe];
[rightSwipe release];
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];
pinch.delegate = self;
[aView addGestureRecognizer:pinch];
[pinch release];
I did some research and to me UIGestureRecognizerDelegate
looked promising and I implemented the delegate method
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
I returned NO for SubView B, hoping that underlying view will get these event. No such luck though
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
if ([gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]] && [gestureRecognizer view] == bView) {
NSLog(@"pinchGesture");
//bView.userInteractionEnabled = NO;
return NO;
}
return YES;
}
Then I disabled user interaction of SubView B inside the delegate callback (commented code in above block), when pinch gesture is recognized, hoping remaining part of gesture will be received by SubView A. No such luck there too..
So that is where I stand now. I have seen this question, where the accepted answer involves property cancelsTouchesInView
. But I believe cancelsTouchesInView
only cancel a particular touch event, do not forward it.
Any other way to achieve my requirement? I am ready to work on whatever hint you provide.
EDIT : BOUNTY TIME
My so called subView A is actually an instance of a 3rd party library's View class, which takes all touches away and I don't have any control over any gestures on it. I want different implementation for left and right swipe and I want pinch, tap etc work just like it is working with this third party view. So I put a view on top of A (SubView B) to get left and right swipes. But now I want to forward other gesture events to underlying library.