4

I'm trying to override hitTest:withEvent in a custom view. This is a container view with many subviews.

Attached to this "superView" is a gesture recognizer. On top of this view sit many subviews (which in turn have more and more subviews) I decided to over ride hitTesh:withEvent in order to decide which view should get the events - and in which cases the superview should get the event in order to trigger the gesture.

Problem I am having is that hitTest will return the superview in cases where a tap gesture would be initialized in a subview. In that case I would like to "retroactively" disregard the tap action - and again "invoke" hitTest:withEvent on the superview. the superview could then decide to pass the "true" responder to this event.

How can I do it:

Example:

// In the container view

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

// the default view that would have been returned
  UIView *v = [super hitTest:point withEvent:event];

  // if nil than touch wasn't in this view so disregard
  if (v == nil)
  {
    return nil;
  }

  // if the touch was on a control than I don't want to take care of it
  if ([v isKindOfClass:[UIControl Class]])
  {
    return v;
  }

  // now here is where I need to fix

  // I am only interested in a touch event that would initiated a pan gesture
  // if the event was a tap for intance - than return [super hitTest:point withEvent:event]
  // for instance if a uitableview cell was tapped - I don't want to mask the event
  if (event wouldn't initiate a pan)
  {
    return v;
  }
  return self;
}
Avba
  • 14,822
  • 20
  • 92
  • 192
  • I don't quite understand your view hierarchy but this might help you http://stackoverflow.com/questions/4961386/event-handling-for-ios-how-hittestwithevent-and-pointinsidewithevent-are-r/4961492#4961492 – Kunal Balani Feb 23 '14 at 21:29
  • I understand how hit test works, but you can imagine a case where a superview contains a tableview - normally the tableview would scroll in cases of panning - but if the touch was initiated in a certain portion of the view - the superview wants to "kidnap" the touch events and do something else with the panning - now my problem is when the superview kidnaps the panning and then wants to re-initiate the events to the tableview - for instance if the initial touch would have triggered a "tap" and not a pan - the superview would like to disregard the event - and let the tableview take care of it – Avba Feb 24 '14 at 09:17

0 Answers0