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

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

问题描述

我有一个简单 - 平凡的 - 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

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

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