如何在 Objective C 中制作视图部分?
老问题标题:如何捕获父UIView中的事件,例如HUD图层?
我正在尝试使用 UI 框架而不是 cocos2d 编写游戏。
我想制作视图部分,并正确处理所有事件。这似乎不可能。
通过部分视图,我指的是 Ruby on Rails(和其他语言)允许您将小块的表示层(视图代码)显示在较大的表示层(视图代码)中的方式。
游戏有一个 HUD 层,顶部有分数(和其他元数据),我使用
[self.view addSubview:y]
或 [self.navigationController Push...]
从视觉上看,它会像这样(不按比例):
+----------------------------+
|A |
|+--------+ +------------+ |
||B | |C | |
|| | | | |
|+--------+ | | |
| | | |
| +------------+ |
+----------------------------+
- A 部分有与主菜单(主导航)相关的按钮
- B 部分有分数等
- 按钮C 是主游戏区域
A 部分和 B 部分都是 HUD 层,它们旨在在需要时具有可点击事件。
我不想一遍又一遍地重复相同的代码,所以我觉得在窗口中包含我想要的视图的 HUD 系统应该是实现此目的的最佳方法。
我遇到的问题是,如果我将 HUD.h/HUD.m 文件包含在任何其他视图控制器中,我将无法再接受 C 部分中 A 部分或 B 部分的点击,并且无法使其执行事件操作,因为它总是抱怨该方法在 C 部分中不存在。
如果我在 C 部分视图中,我应该接受来自 A 和 B 的点击并适当地处理它们。
因此,通过 HUD 分层系统捕获事件并适当处理事件的最佳方法是什么?
我见过的最接近的可能是使用touchesBegan,但我不清楚如何将touchesBegan 事件链接到A/B 部分控制器中的事件。
谢谢。
Old question title: How to capture events in parent UIViews, such as HUD layers?
I am attempting to write a game using the UI framework rather than cocos2d.
I would like to make View partials with all their events handled properly. It does not seem possible.
By partial views, I refer to the way Ruby on Rails (and other languages) allows you to have small chunks of a presentation layer (view code) that appear in a larger piece of a presentation layer (view code).
The game has a HUD layer with score at the top (and other meta data) and I use
[self.view addSubview:y]
or[self.navigationController push...]
Visually, it would be like this (not to scale):
+----------------------------+
|A |
|+--------+ +------------+ |
||B | |C | |
|| | | | |
|+--------+ | | |
| | | |
| +------------+ |
+----------------------------+
- Section A has buttons relating to main menu (primary navigation)
- Section B has buttons for score, etc
- Section C is the main game area
Both Section A and Section B are HUD layers, they are meant to have clickable events where required.
I do not want to keep repeating the same code over and over again, so I feel a HUD system where I include the views I want in the window should be the best way to accomplish this.
The problem I am having is that if I include my HUD.h/HUD.m file in any other View controller, I can no longer accept the clicks for Sections A or B in Section C, and I cannot make it perform event actions as it always complains that the method doesn't exist within Section C.
If I am on the Section C view I should accept clicks from A and B and handle them appropriately.
Thus, what is the best way to capture events via a HUD layering system, and handle the events appropriately?
The closest I've seen is possibly using touchesBegan but what I am not clear on is how to link touchesBegan events to events that are in Section A/B's controller.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不是 100% 确定这就是您要问的,但如果您说您希望在 B 或 C 部分上发生点击,但实际上并没有触摸按钮来传递到 A 部分,那么这就是您应该做的做:
创建 UIView 的子类,例如称为 HUDItemContainerView。
重写 UIView 的 -hitTest:withEvent: 方法,如下所示:
这会向 HUDItemContainerView 添加以下行为:“如果我体内的触摸触摸了我的子视图之一,请使用该触摸。如果它直接触摸我(即不是我的子视图之一),则忽略触摸并让它穿过我身后的任何视图(例如 A 部分)。”
然后将 HUDItemContainerView 用于 B 部分和 C 部分的基础视图。
I'm not 100% sure this is what you are asking, but if you're saying you want taps that occur on section B or C that don't actually touch a button to pass through to section A, then here's what you should do:
Create a subclass of UIView, called, for example, HUDItemContainerView.
Override UIView's -hitTest:withEvent: method like this:
This adds the following behavior to HUDItemContainerView: "If a touch within me touches one of my subviews, use that touch. If it touches me directly (i.e. not one of my subviews), then ignore the touch and allow it to pass through to whatever views are behind me (e.g. section A)."
Then use HUDItemContainerView for Section B and Section C's base views.
我决定测试几种不同的想法来使用 cocos2d,因为这将是一件好事,可以增加我的技能。
关闭。
I've decided having tested several different ideas to go with cocos2d as it would be a good thing to add to my skillset.
Closed.