问题
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];
}
回答1:
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;
}
}
回答2:
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
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[viewC touchesBegan:touches withEvent:event];
// Note this could be in sibling B or in parent A, depending on which
// has a reference to the 'viewC' object
}
来源:https://stackoverflow.com/questions/16067865/propagating-touch-events-through-sibling-subviews