如何防止 UIView 消耗用户输入 [英] How to prevent a UIView from consuming user input

查看:22
本文介绍了如何防止 UIView 消耗用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的视图层次结构示例.

I have a simple view hierarchy example.

显然,主视图空间是用户将与之交互的主要空间.在底部,我有可以弹出的选项卡,以向用户指示他/她在应用程序进程中的位置.通常,这些选项卡仅占用底部自定义选项卡"矩形指示的空间,但它们可以一直向上扩展以填充空白空间"框.

Obviously the main view space is the primary space the user will interact with. At the bottom I have tabs that can pop up to indicate to the user where he/she is in the progression of the app. Normally, these tabs only take up the space indicated by the "Custom Tabs" rectangle at the bottom, but they can expand all the way up to fill the "Empty Space" box.

为了使选项卡仍然可点击,我必须使选项卡视图的框架成为包含自定义选项卡"空间和空白空间"空间的完整矩形.这导致空白空间"在选项卡未弹出时无法与用户交互,因为输入基本上由该 UIView 使用,而不是通过层次结构的其余部分转发.

In order for the tabs to still be clickable, I had to make the tab view's frame the full rectangle containing both the "Custom Tabs" space and "Empty Space" space. What this results in is that "Empty Space" not being interactive to the user when the tabs aren't popped up, because the input is basically being consumed by that UIView, and not forwarded through the rest of the hierarchy.

我想这个问题的根源在于主视图空间"和空白空间+自定义选项卡"都是主窗口的子视图.

I suppose the root of this problem is that both "Main View Space" and the "Empty Space + Custom Tabs" are both subviews of the main window.

如果用户没有主动点击交互元素,有没有办法告诉系统将用户输入转发到同级视图?例如,使用 touchesBegantouchesEnded 等方法向操作系统表明此视图未使用输入.

Is there a way I can tell the system to forward the user input to the sibling views if the user didn't actively tap on an interactive element? For example, doing something with the touchesBegan, touchesEnded etc. methods that would indicate to the OS that this view did not use the input.

编辑

这是视图的另一个版本,展示了一个选项卡打开的状态:

Here's another version of the view, demonstrating the tate of one tab being open:

EDIT2

经过一些简单的测试,似乎默认行为是最顶层的视图首先获取输入.即使您在 UITextField 之上有一个清晰的 UIView,这也适用.清除 UIView 将消耗输入,防止 UITextField 可编辑

After some simple testing, it would seem that the default behavior is that the top most view gets the input first. This applies even if you have a clear UIView on top of a UITextField. The clear UIView will consume the input, preventing the UITextField from being editable

EDIT3

标签应该工作的方式是用户可以点击标签(大小如第一张图片所示),然后它会展开以显示与该标签关联的缩略图视图(如第二张图片所示).然后,用户可以选择再次点击选项卡将其关闭,并将大小恢复为原始图片.为了使选项卡在打开时可点击,我必须让包含视图基本上足够大以包含所有 4 个选项卡,就好像它们是打开的一样.这会导致包含视图中有很多空白空间.这个空白空间导致屏幕上基本上死的输入空间.如果主视图空间中有一个按钮被空白空间覆盖,用户将无法点击它.我希望能够避免这种行为,并使空白区域覆盖的那个按钮仍然可以点击.

The way the tabs are supposed to work is the user can tap on a tab (sized as in the first picture), and then it will expand to display a thumbnail view associated with that tab (as in the second picture). The user can then optionally tap the tab once more to close it, and return the size to the original picture. In order for the tab to be clickable when it is open, I have to have the containing view be basically large enough to contain all 4 tabs as if they were open. This results in a lot of empty space in the containing view. This empty space results in essentially dead input space on the screen. If there were a button in the main view space that is covered by the empty space, the user would not be able to click on it. I would like to be able to avoid that behavior, and have that button covered by the empty space still be clickable.

推荐答案

与其尝试向前"触摸,我会修改您的布局,以便选项卡视图仅与选项卡一样大,并将其更改为 .frame 仅在您需要时才成为代码中较大的矩形.例如,当一个标签被点击时:

Rather than trying to "forward" touches, I would modify your layout so that the tab view is only as big as the tabs, and change it's .frame to be the larger rectangle in code only when you need it. For example, when a tab is clicked:

 CGRect tabFrame = tabView.bounds;
 tabFrame.origin.y = top_of_emptySpace;
 tabFrame.size.height = height_of_emptySpace + height_of_tabView;
 tabView.frame = tabFrame;

然后你可以添加你需要的内容.当你需要它消失时,删除内容然后做:

then you can add the content you need. when you need it to go away, remove the content then do :

 CGRect tabFrame = tabView.bounds;
 tabFrame.origin.y = top_of_tabView;
 tabFrame.size.height = height_of_tabView;
 tabView.frame = tabFrame;

可能需要进行一些调整才能使内容随心所欲地显示出来,但这样,当标签页最小化时,您无需做任何额外的事情就可以使主视图正确响应触摸.

There might be some tweaking required to make the content show up as you like, but this way, when the tabs are minimized, you won't have to do anything extra to make the main view respond to touches correctly.

这篇关于如何防止 UIView 消耗用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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