Why is hitTest not being called when views subview is touched?

痞子三分冷 提交于 2020-01-13 14:13:53

问题


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:


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.



来源:https://stackoverflow.com/questions/15466507/why-is-hittest-not-being-called-when-views-subview-is-touched

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!