如何在父母uiview之外部分访问uiview子视图? [英] How access an uiview subview partially outside of his parent uiview?

查看:121
本文介绍了如何在父母uiview之外部分访问uiview子视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下UIVIew架构(x,y,宽度,高度):

I have the following UIVIew architecture (x,y,width,height) :

- MainWindow (0,0,768,1024)
    - MainView (0,0,768,80) 
        - containerview (500,40,120,80)
            - subview (500,40,120,80) 
                -some buttons

我的问题是子视图的底部位于MainView的边界之外。子视图底部的按钮没有响应。顶部的那个是响应式的,因为它们的位置也在Mainview内部。

My problem is that the bottom of the subview lay outside of the bound of MainView. Buttons at the bottom of subview are not responsive. The one at the top are responsive, because their position is also inside on Mainview.

所以当我尝试点击子视图底部的按钮时,我实际点击了MainWindow!子视图的底部按钮的位置不在MainView内部

So when i try to click the buttons at the bottom of subview i actually click on MainWindow! Position of bottoms buttons of subview are not inside of MainView

有没有办法让我的所有子视图都可用,即使它的一半在MainView绑定之外?

Is there a way to make all my subview available even if half of it is outside of MainView bound?

我知道我可以直接在MainWindow下创建子视图,但我不想重做我的代码。

I know that i can create the subview directly under MainWindow instead, but i don't want to redo my code.

更新
这里如何设计我的观点:$ b​​ $ b A = MainWindow,B = MainView,C =容器视图,D =子视图,X =我要点击的位置

Update Here how is design my views : A = MainWindow, B = MainView, C = container view, D = subview, X = where i want to click

+----------------------------+
|A                           |
|+-------------------------+ |
||B                        | |
||            +----------+ | |
|+------------|C&D       |-+ |
|             |X         |   |
|             +----------+   |
+----------------------------+

感谢你

推荐答案

你需要实现 hitTest:(CGPoint)指向你的MainView中的事件:(UIEvent *)事件

查看文档

位于接收者界限之外的点永远不会被报告为
点击,即使它们实际上位于接收者的一个子视图中。
如果
父视图的clipsToBounds属性设置为NO,则子视图可以在视觉上超出其父级的范围。但是,点击
测试总是​​忽略父视图边界之外的点。

Points that lie outside the receiver’s bounds are never reported as hits, even if they actually lie within one of the receiver’s subviews. Subviews may extend visually beyond the bounds of their parent if the parent view’s clipsToBounds property is set to NO. However, hit testing always ignores points outside of the parent view’s bounds.

发表评论:

你应该是UIView的子类。然后将nib中的MainView类设置为UIView子类。

You should subclass UIView. Then set the class of MainView in the nib to your UIView subclass.

这里讨论了一些hittesting ,但我无法找到它的工作原理。

Some hittesting is discussed here, but I wasn't able to find clear explanation of how it works.

试试这个StackOverflow问题

我还没有测试过,但以下代码可能会满足您的需求:

I haven't tested it, but the following code might do what you need:

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    for(UIView *subview in self.subviews)
    {
        UIView *view = [subview hitTest:[self convertPoint:point toView:subview] withEvent:event];
        if(view) return view;
    }
    return [super hitTest:point withEvent:event];
}

这篇关于如何在父母uiview之外部分访问uiview子视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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