Suppose I have a UIViewController subclass that takes care of some UIViews. These UIViews are added as subviews of the UIViewController's view
property):
UIViewController:
- (void)viewDidLoad {
UIImageView *smallImageView...
[self.view addSubview:smallImageView];
UIButton *button..
[self.view addSubview:button];
UIView *bigUIView.. // covers the entire screen (frame = (0.0, 0.0, 1024.0, 768.0))
[self.view addSubview:bigUIView];
...
}
AFAIK, since the bigUIView
is the frontmost view and covers the entire screen, it will receive the touchesBegan:withEvent:
and the other views, such as button
won't receive any touch event.
In my application bigUIView
must be the topmost view because it holds the main user interface objects (CALayers, actually, the main game objects) that, when animated, must be on top of all other secondary UI elements (UIButtons, etc). However, I'd like to be able to still have the UIButtons and other objects in the Responder Chain.
I tried implementing the following in the bigUIView
class:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.superview touchesBegan:touches withEvent:event];
... hit test the touch for this specific uiview..
}
Notes:
bigUIView
's superview refers to the UIViewController'sview
property, does it propagate the touch event to all of it's subviews?bigUIView
must haveuserInteractionEnabled = YES
because it also handles user input.- I can't bring the
button
/smallImageView
to front because it would appear above the main game objects (sublayers ofbigUIView
).
Thanks in advance.
pointInside:withEvent:
is the solution. By returning NO when the point is inside the frame rectangle of one of the other subviews cause thebigUIView
's-touchesBegan:withEvent:
method not to be called and thebutton
's action being triggered. Note that this cause a coupling between thebigUIView
and thebutton
. How can I intercept the event phase methods only in theUIViewController
when thebigUIView
cover the whole screen? Creating a different method signature inbigUIView
class and have it delegated by the "catch all"UIViewController
class? – Stannary