frangulyan
frangulyan

Reputation: 3847

iOS: pass gesture to views underneath

I have a simple setup: there are 2 sibling UIViews A and B, B fully covers A (but it is transparent, so A is visible):

┌──────────────────┐
│   View A         │
│                  │
│   ┌──────────────┴───┐
│   │   View B         │
│   │                  │
│   │                  │
│   │                  │
│   │                  │
│   │                  │
└───┤                  │
    │                  │
    │                  │
    └──────────────────┘

I need 2 different UIGestureRecognizers: A should have a tap recognizer and B should have a pan recognizer. Whenever I tap on B it should pass the touch events to view A behind so that it can recognize a tap (I will use some logic to understand if pan was accepted or not, like distance, start area, etc). Is this possible to do in iOS?

My experience so far is that once B accepts a touch it will never forward it to A which is behind. And the decision to accept or not should be done immediately in overriden pointInside or hitTest functions. However, in my case the decision is based on some logic and depends on the context and history of user interaction.

Upvotes: 3

Views: 1709

Answers (5)

Pankaj Kulkarni
Pankaj Kulkarni

Reputation: 561

By setting isUserInteractionEnabled to false on view B it passes the user interactions to view A.

Upvotes: -1

user1055568
user1055568

Reputation: 1429

Add a Tap recognizer to both A and B. Then you can disambiguate the Tap and the Pan within B using whatever logic you need. Your Tap handler can use the gesture.view to see whether it came from A or B, and convert the touch to A coordinates in either case.

Upvotes: 0

Anjan
Anjan

Reputation: 31

i found this one add this class to top view

@interface ClickthroughView : UIView
@end

@implementation ClickthroughView
-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    return NO;
}
@end

Upvotes: -1

Janmenjaya
Janmenjaya

Reputation: 4163

As per Responder Chain it will forwards the event to next view (or next level), if it is not handled by the top object, but if it is handled by top object then it won't pass that to next level. So in your case if view B is responding the touch event it will not pass the touch to view A, but yes if you forcefully block the touch event in view B, then the Responder Chain passes the touch event to view A.

More is here in the link : enter link description here

hope it helps

Upvotes: 1

Rob
Rob

Reputation: 437381

If B covers A, it's going to prevent A from receiving gestures. But why do you have this transparent B over A? Get rid of B and put both gestures on A. And then you can define the tap gesture to require the pan gesture to fail.

Upvotes: -1

Related Questions