8

I have a view which contains several subviews which are complex controls with several buttons. The superview has gesture recognizers for taps, swipes etc.

In some cases, when receiving a single or double touch I would like the superview to pass the gesture recognizer to the subview for treatment.

For instance:

singletaprecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSingleTapGestureRecognizerDetected:)];

- (void)onSingleTapGestureRecognizerDetected:(UITapGestureRecognizer*)theTapGestureRecognizer
{
    if (someCaseHappens) {
        // do something with the gesture - for instance - move a control from one place to another or some other UI action in the superview
    }
    else {
        // the subview will need to get the gesture and do something with it (imagine the touch was inside a button in a subview - the top superview should disregard the gesture and the button should be pressed) - THIS ISN"T HAPPENING NOW
    }
}
Gavin
  • 8,204
  • 3
  • 32
  • 42
Avba
  • 14,822
  • 20
  • 92
  • 192

1 Answers1

7

Well, you could create a custom view inheriting from UIView and then override:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

From this method you can return the view you want to handle the event.

take a look at: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/hitTest:withEvent:

EsbenB
  • 3,356
  • 25
  • 44
  • I'm not sure this is the best way. How do the apple classes handle multiple gestures on the same view? How would it know to pass the event to a subview without knowing of the subviews before hand – Avba Jan 02 '14 at 19:21
  • You need to let your most superview (the one with the gesture recognizers) implement this method. It will already have the subviews in question. In some cases you simply handle the call to hitTest my calling super, and in other cases you are returning the view you want to be handled. That way you "simulate" the user is touching a another view – EsbenB Jan 02 '14 at 19:36
  • How would you know what gesture cause the hitTest – Avba Jan 02 '14 at 23:30
  • You can't know that, when the hitTest comes around, you need to combine the info you are getting from the gestures with the hittest. see also http://stackoverflow.com/questions/259183/delay-with-touch-events – EsbenB Jan 03 '14 at 10:23