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;
}
4

0 回答 0