界面视图.为什么在其父范围之外的子视图没有收到触摸? [英] UIView. Why Does A Subviews Outside its Parent's Extent Not Receive Touches?

查看:28
本文介绍了界面视图.为什么在其父范围之外的子视图没有收到触摸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 - 琐碎的 - UIView 父/子层次结构.一位家长 (UIView).一个孩子(UIButton).父边界小于子边界,因此子边界的一部分超出了父边界.

I have a simple - trivial - UIView parent/child hierarchy. One parent (UIView). One child (UIButton). The parents bounds are smaller then it's child's bounds so that a portion of the child extends beyond the bounding box of its parent.

这里的问题是:父 bbox 之外的孩子的那些部分接收触摸.只有在父级的 bbox 内点击才允许子级按钮接收触摸.

Here's the problem: Those portions of the child outside the bbox of the parent do not receive touches. Only tapping within the bbox of the parent allows the child button to receive touches.

有人可以建议修复/解决方法吗?

Can someone please suggest a fix/workaround?

对于那些关注这个问题的人,这是我作为@Bastians 最优秀答案的结果而实施的解决方案:

For those following this question, here is the solution I implemented as a result of @Bastians most excellent answer:

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {

    BOOL isInside = [super pointInside:point withEvent:event];

    // identify the button view subclass
    UIButton *b = (UIButton *)[self viewWithTag:3232];
    CGPoint inButtonSpace = [self convertPoint:point toView:b];

    BOOL isInsideButton = [b pointInside:inButtonSpace withEvent:nil];

    if (isInsideButton) {

        return isInsideButton;

    } // if (YES == isInsideButton)

    return isInside;        
}

推荐答案

问题在于响应者链.当您触摸显示屏时,它会从父母下降到孩子.

The problem is the responder chain. When you touch the display it will go down from the parents to the childen.

所以..当你触摸屏幕时,父母会看到触摸超出了自己的范围,所以孩子们甚至不会问.

So .. when you touch the screen the parent will see that the touch is outside of it's own bounds and so the children will not even asked.

执行此操作的函数是 hitTest.如果你有自己的 UIView 类,你可以覆盖它并自己返回按钮.

The function that does that is the hitTest. If you have your own UIView class you can overwrite it and return the button by yourself.

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

这篇关于界面视图.为什么在其父范围之外的子视图没有收到触摸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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