为什么在触摸视图子视图时未调用hitTest? [英] Why is hitTest not being called when views subview is touched?

查看:201
本文介绍了为什么在触摸视图子视图时未调用hitTest?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 parentView 的视图,该视图有一个名为 childView 的子视图。 childView 的一部分超出了 parentView childView 已附加 panGestureRecognizer 。我在 parentView 中实现了以下内容,即使它在其超级视图范围之外,它也可以识别对 childView 的触摸:

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

但是当我触摸或拖动 childView hitTest 甚至没有在 parentView 上调用。为什么会这样?

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

推荐答案

因为事件沿着响应者链进行,并且在调用hittest之前被使用

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:

这不是虽然很清楚:D
http ://developer.apple.com/library/ios/#DOCUMENTATION/EventHandling/Conceptual/EventHandlingiPhoneOS/Introduction/Introduction.html

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

但重要的一点:


  • 点击测试视图是第一个处理触摸事件的机会。如果命中测试视图无法处理事件,则事件将按照响应者链由响应者对象组成中的描述在该视图的响应者链中向上传播,直到系统找到可以处理该事件的对象为止。

  • 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.

这篇关于为什么在触摸视图子视图时未调用hitTest?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆