5

I have a view which I'll call parentView which has a subview called childView. Part of childView is outside the bounds of parentView, and childView has a panGestureRecognizer attached to it. I have implemented the following in parentView so that it will recognize touches to childView even though it's outside of its superviews bounds:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if (!self.clipsToBounds && !self.hidden && self.alpha > 0)
    {
        for (UIView *subview in self.subviews)
        {
            CGPoint subPoint = [subview convertPoint:point fromView:self];
            UIView *result = [subview hitTest:subPoint withEvent:event];
            if (result != nil)
            {
                return result;
                break;
            }
        }
    }

    return [super hitTest:point withEvent:event];
}

Yet when I touch or drag childView, hitTest is not even being called on the parentView. Why is this?

Ser Pounce
  • 14,196
  • 18
  • 84
  • 169

1 Answers1

3

because the event goes down the responder chain and is used before hittest gets called

so the event goes from top to bottom in this case... check out the documentation concerning the responder chain:

it is not very clear though :D http://developer.apple.com/library/ios/#DOCUMENTATION/EventHandling/Conceptual/EventHandlingiPhoneOS/Introduction/Introduction.html

BUT the important bit:

  • The hit-test view is given the first opportunity to handle a touch event. If the hit-test view cannot handle an event, the event travels up that view’s chain of responders as described in “The Responder Chain Is Made Up of Responder Objects” until the system finds an object that can handle it.

  • Touch events. If the hit-test view cannot handle a touch event, the event is passed up a chain of responders that starts with the hit-test view.

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • The resource is outdated. Check out https://developer.apple.com/library/archive/documentation/General/Conceptual/Devpedia-CocoaApp/EventHandlingiPhone.html#//apple_ref/doc/uid/TP40009071-CH13-SW6 – Nico Apr 16 '21 at 16:02
  • the answer is still 100% valid for the question and the mechanism didnt change and I dont think it ever will -- the fact that you point to a retired doc doesnt change it ;) – Daij-Djan Apr 27 '21 at 09:31
  • your link shows "This document may not represent best practices for current development" -- MAY :D -- what exactly is invalid about the chain? obviously this is for UIKit – Daij-Djan Apr 27 '21 at 09:32
  • 2
    I never claimed it's not true. It is indeed. I just provided a link to the resource because the other one just redirects you to the standard "UIKit" site of Apple. – Nico Apr 27 '21 at 13:11