Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forwarding UIGesture to views behind

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). 

enter image description here

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.

  1. SubView B should receive all swipe and tap (single and double) gestures.
  2. 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.

like image 819
Krishnabhadra Avatar asked Feb 09 '12 11:02

Krishnabhadra


2 Answers

If i undestand your problem correct, you may just add another, clear view with rect, same as you A and B view, and implement all gesture on it: when you do pinch gesture, control subView A, when swipe and tap (single and double) gestures - control subView B. You can do it different ways: via pointers or just sending recived gesture to method in class, wich controls your sub view.

for example:

UISwipeGestureRecognizer *leftSwipe  =  [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(leftSwipe:)]; [leftSwipe setDirection:(UISwipeGestureRecognizerDirectionLeft)]; leftSwipe.delegate  =   subViewAcontroller; [clearView addGestureRecognizer:leftSwipe]; [leftSwipe release];  UISwipeGestureRecognizer *rightSwipe  =  [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(rightSwipe:)]; [rightSwipe setDirection:(UISwipeGestureRecognizerDirectionRight)]; rightSwipe.delegate   =   subViewAcontroller; [clearView addGestureRecognizer:rightSwipe]; [rightSwipe release];  UIPinchGestureRecognizer *pinch   =  [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)]; pinch.delegate    =   subViewBcontroller; [clearView addGestureRecognizer:pinch]; [pinch release]; 

or:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{     if ([gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]]) {         NSLog(@"pinchGesture");         [subViewBcontroller solvePinchGesture: gestureRecognizer];     } //etc     return YES; } 
like image 92
SentineL Avatar answered Sep 28 '22 09:09

SentineL


The pinch gesture will Never received by the aView, because you view hierarchy is incorrect.

---> A superView   |  ---> SubView - A   |  ---> SubView - B (exactly on top of A, completely blocking A). 

bView will captures all multi-touches including the pinch gesture, but bView will not handle the pinch gesture, so it will pass it to Next responder in the Responder chain, i.e. the superView of bView or the viewController of bView. The pinch event will never passed to its sibling views, because they are in parallel (no parent-child or view-viewController relationship).

You can change your view hierarchy as below:

---> A superView   |  ---> SubView - A         |        ---> SubView - B (child view of b, exactly on top of A, completely blocking A)    - (void)viewDidLoad {     [super viewDidLoad];      aView = [[UIView alloc] initWithFrame:self.view.bounds];     aView.backgroundColor = [UIColor blueColor];      bView = [[UIView alloc] initWithFrame:self.view.bounds];     bView.backgroundColor = [UIColor redColor];      [self.view addSubview:aView];     [aView addSubview:bView];      UISwipeGestureRecognizer *leftSwipe  =  [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(leftSwipe:)];     [leftSwipe setDirection:(UISwipeGestureRecognizerDirectionLeft)];     leftSwipe.delegate  =   self;     [bView addGestureRecognizer:leftSwipe];      UISwipeGestureRecognizer *rightSwipe  =  [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(rightSwipe:)];     [rightSwipe setDirection:(UISwipeGestureRecognizerDirectionRight)];     rightSwipe.delegate   =   self;     [bView addGestureRecognizer:rightSwipe];      UIPinchGestureRecognizer *pinch   =  [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];     pinch.delegate    =   self;     [aView addGestureRecognizer:pinch]; } 

Please get more information from below link: https://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/EventsiPhoneOS/EventsiPhoneOS.html#//apple_ref/doc/uid/TP40009541-CH2-SW1

--> Important note of the event passing in the responder chain "When the system delivers a touch event, it first sends it to a specific view. For touch events, that view is the one returned by hitTest:withEvent:; for “shaking”-motion events, remote-control events, action messages, and editing-menu messages, that view is the first responder. If the initial view doesn’t handle the event, it travels up the responder chain along a particular path:

  1. The hit-test view or first responder passes the event or message to its view controller if it has one; if the view doesn’t have a view controller, it passes the event or message to its superview.
  2. If a view or its view controller cannot handle the event or message, it passes it to the superview of the view.
  3. Each subsequent superview in the hierarchy follows the pattern described in the first two steps if it cannot handle the event or message.
  4. The topmost view in the view hierarchy, if it doesn’t handle the event or message, passes it to the window object for handling.
  5. The UIWindow object, if it doesn’t handle the event or message, passes it to the singleton application object."

my answer above is not the only solution, what you need to do is make swipe and pinch gesture handling logics in the same responder chain, so the un-handle pinch gesture event will passed to the correct responder. For example, you can omit subview b, and add the swipe left/right gestures to superView of aView.

like image 33
flypig Avatar answered Sep 28 '22 10:09

flypig