How to make view partials in Objective C?

203 views Asked by At

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.

2

There are 2 answers

0
zardon On BEST ANSWER

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.

2
Josh Rosen On

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:

- (UIView *)hitTest:(CGPoint)point 
          withEvent:(UIEvent *)event
{
    UIView* touchedView = [super hitTest:point withEvent:event];

    if (touchedView == self)
        return nil;

    return touchedView;
}

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.