Nico
Nico

Reputation: 6359

Adding a UIGestureRecognizer taking priority over all other interactions

When I tap on a UIButton, a UIView MyView appear from the bottom a cover a third of the screen. I would like that when I tap somewhere outside this view, it disappears.

I thought about adding another transparent UIView right under MyView and add a tab gesture on it with the dismiss function but I'm sure there is something cleaner than this.

So I thought about adding the tap gesture MyTapGesture to dismiss MyView on self.view of the UIViewController. The problem is that outside this view, I have other UIControls and gestures that capture also any touch at the same time than MyTapGesture.

How can I make MyTapGesture the priority gesture outside MyView and ignore all other gesture, taps, etc...?

Upvotes: 1

Views: 2642

Answers (1)

Mukesh
Mukesh

Reputation: 3690

You may have to use the gesture delegate methods to handle two tapGestureRecognizer activate the one you need depending on scenario

#pragma mark - UIGestureRecognizerDelegate methods
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {

return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
   if ([tapGestureRecognizer1 isEqual:gestureRecognizer]) {

   return [tapGestureRecognizer2 isEqual:otherGestureRecognizer];
   }

 if ([tapGestureRecognizer2 isEqual:gestureRecognizer]) {

return [tapGestureRecognizer1 isEqual:otherGestureRecognizer];
}

return NO;
}

Upvotes: 1

Related Questions