Why is hitTest not being called when views subview

2019-05-09 16:05发布

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?

1条回答
乱世女痞
2楼-- · 2019-05-09 16:43

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.

查看更多
登录 后发表回答