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];
}
One possibility is to overwrite the
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
method in view B. You can make it return only if a subview of B is hit. Try it like this:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *hitView = [super hitTest:point withEvent:event];
if (hitView == self) {
return nil;
} else {
return hitView;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With