I've got a stack of subviews that are all have user interactive sections (children) and all full screen. The problem is that, if I touch down on a non-interactive section at the top of the stack, it won't then propagate that touch across the rest of the stack. My setup:
view A --view B (full screen container, not itself interactive but has interactive subviews) ----view B1 (interactive) ----view B2 (interactive) --view C (same as B) ----view C1 (interactive) ----view C2 (interactive)
B and C are both full screen, but B1/B2/C1/C2 are only small sections of the screen.
[a addSubview:b];
[a addSubview:c];
If I touch anything outside of C1/C2, I'd like the touch event to then check if it hit anywhere inside of B (B1/B2), but instead it just goes back to A, and then to A's parent. Is it possible to do this? If I set userInteractionEnabled NO on C but YES on C1/C2, it doesn't get any calls to the inner ones either, although in this case then B would get the touches, as expected.
edit: Ended up traversing the view stack manually to check only for certain subviews and not all of them:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
if (self != self.topCustomViewsContainer) {
for (UIView *v in self.createdSubviews) {
CGPoint newPoint = point;
newPoint.x -= v.frame.origin.x;
newPoint.y -= v.frame.origin.y;
UIView *hit = [v hitTest:newPoint withEvent:event];
if (hit)
return hit;
}
return nil;
}
return [super hitTest:point withEvent:event];
}
Events propagate up the view hierarchy from child to parent (as you discovered). So two possibilities occur to me:
Make C a child of B. They both have the same size, and C obviously already has a transparent background, so add it to B and use
[viewB bringSubviewToFront:viewC]
to make it the first to receive touches. Anything it doesn't capture will pass to views underneath it, then up to parent A.Or: Manually capture touches, and force them over to view B (sibling of C). This involves implementing this in your view controller class
One possibility is to overwrite the
method in view B. You can make it return only if a subview of B is hit. Try it like this: