允许与另一个UIView下的UIView进行交互 [英] Allowing interaction with a UIView under another UIView

查看:111
本文介绍了允许与另一个UIView下的UIView进行交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个简单的方法允许与位于另一个UIView下的UIView中的按钮交互 - 在按钮顶部没有实际的对象从顶部的UIView?



例如,目前我有一个UIView(A),顶部有一个对象,屏幕底部有一个对象,中间没有任何东西。这坐在另一个UIView的顶部有按钮在中间(B)。但是,我似乎不能与B中间的按钮交互。



我可以看到B中的按钮 - 我设置了A的背景clearColor - 但是B中的按钮似乎没有接收到触摸,尽管事实上没有来自A的对象实际上在这些按钮的顶部。



编辑 - 我仍然想要能够与顶部UIView中的对象交互



当然有一个简单的方法来做这个?

解决方案

您应该为顶视图创建一个UIView子类,并覆盖以下方法:

   - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
//如果我们返回NO
,UIView对于触摸事件将是 point.y< MIDDLE_Y1 || point.y> MIDDLE_Y2);
}

您还可以查看hitTest:event:

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button?

For instance, at the moment I have a UIView (A) with an object at the top and an object at the bottom of the screen and nothing in the middle. This sits on top of another UIView that has buttons in the middle (B). However, I cannot seem to interact with the buttons in the middle of B.

I can see the buttons in B - I've set the background of A to clearColor - but the buttons in B do not seem to receive touches despite the fact that there are no objects from A actually on top of those buttons.

EDIT - I still want to be able to interact with the objects in the top UIView

Surely there is a simple way of doing this?

解决方案

You should create a UIView subclass for your top view and override the following method:

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    // UIView will be "transparent" for touch events if we return NO
    return (point.y < MIDDLE_Y1 || point.y > MIDDLE_Y2);
}

You may also look at the hitTest:event: method.

这篇关于允许与另一个UIView下的UIView进行交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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