Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Propagating touch events through sibling subviews?

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];
}
like image 921
puzzl Avatar asked Apr 17 '13 18:04

puzzl


1 Answers

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;
  }
}
like image 130
bas Avatar answered Nov 06 '22 07:11

bas