31

Basically, here is my view hierarchy (and I appologize if this is hard to read... I'm new here so posting suggestions happily accepted)


--AppControls.xib
-------(UIView)ControlsView
----------------- (UIView)TopBar
----------------- -------------- btn1, btn2, btn3
----------------- UIView)BottomBar
----------------- --------------slider1 btn1, btn2
--PageContent.xib
----------------- (UIView)ContentView
----------------- --------------btn1, btn2, btn3
----------------- --------------(UIImageView)FullPageImage


My situation is that I want to hide and show the controls when tapping anywhere on the PageContent thats not a button and have the controls show, much like the iPhone Video Player. However, when the controls are shown I still want to be able to click the buttons on the PageContent.

I have all of this working, except for the last bit. When the controls are showing the background of the controls receives the touch events instead of the view below. And turning off user interaction on the ControlsView turns it off on all its children.

I have tried overriding HitTest on my ControlsView subclass as follows which I found in a similar post:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
UIView *hitView = nil;
NSArray *subviews = [self subviews];
int subviewCount = [subviews count];
for (int subviewIndex = 0; !hitView && subviewIndex < subviewCount; subviewIndex++){
hitView = [[subviews objectAtIndex:subviewIndex] hitTest:point withEvent:event];
}   
return hitView;
}

However, at this point my slider doesn't work, nor do most of the other buttons, and really, things just start getting weird.

So my question is in short: How do I let all the subviews of a view have touch events, while the super view's background is unclickable, and the buttons on views below can receive touch events.

Thanks!

4 Answers 4

77

You're close. Don't override -hitTest:withEvent:. By the time that is called, the event dispatcher has already decided that your subtree of the hierarchy owns the event and won't look elsewhere. Instead, override -pointInside:withEvent:, which is called earlier in the event processing pipeline. It's how the system asks "hey view, does ANYONE in your hierarchy respond to an event at this point?". If you say NO, event processing continues below you in the visible stack.

Per the documentation, the default implementation just checks whether the point is in the bounds of the view at all.

Your strategy is to say "yes" when any of your subviews is at that coordinate, but say "no" when the touch would be hitting the background.

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    for (UIView * view in [self subviews]) {
        if (view.userInteractionEnabled && [view pointInside:[self convertPoint:point toView:view] withEvent:event]) {
            return YES;
        }
    }
    return NO;
}
8
  • 1
    You're welcome. I fought with this same issue for many hours once. Happy to help.
    – Ben Zotto
    Commented Aug 6, 2010 at 21:25
  • 6
    Sure. :) I don't see display hierarchies modeled so completely on SO very often, but it's useful to see it like that. FYI, here's a fun debugging tip: in gdb when stopped at a breakpoint, try running po [myView recursiveDescription]. It dumps out the whole hierarchy from that point down.
    – Ben Zotto
    Commented Aug 6, 2010 at 21:39
  • 1
    This answer put an end to a long, tortuous journey to allow for overlapping views to handle the events. Thank you so much.
    – supertodda
    Commented Dec 2, 2010 at 23:09
  • 1
    Brilliant. Thanks a lot. I wonder how come such a simple behavior isn't already part of UIKit.
    – Ben G
    Commented Mar 3, 2012 at 16:35
  • 1
    Note - this may behave in unexpected ways if a child extends beyond the boundary of the parent and the CGpoint is in the child but outside the parent. A quick test shows that w/ stock behavior, such a point is considered outside, but with this modification it's considered inside. i don't have room for the code in the comments here, so i'll post another answer. Commented Sep 13, 2012 at 15:20
11

Thanks to @Ben Zutto, Swift 3 solution:

override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
    for view in self.subviews {
        if view.isUserInteractionEnabled, view.point(inside: self.convert(point, to: view), with: event) {
            return true
        }
    }

    return false
}
2
  • 1
    or return subviews.first { $0.isUserInteractionEnabled && $0.point(inside: convert(point, to: $0), with: event) } != nil Commented Jul 4, 2018 at 2:29
  • note: in order for this to work properly the parent's User Interaction also needs to be enabled
    – Merricat
    Commented Jul 7, 2019 at 23:38
1

Another approach may be to have an invisible full-screen button behind everything else, and take appropriate action when it is hit.

1
  • 3
    That sounds like a terrible idea. Commented Aug 24, 2017 at 1:00
1

A slight variant on Ben's answer, dealing w/ children which extend outside their parent. If clipChildren is YES, then this will not return YES for points which are outside the main control but inside some child. if clipChildren is NO, this is the same as Ben's.

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    BOOL clipChildren = YES;

    if (!clipChildren || [super pointInside:point withEvent:event]) {
        for (UIView * view in [self subviews]) {
            if (view.userInteractionEnabled && [view pointInside:[self convertPoint:point toView:view] withEvent:event]) {
                return YES;
            }
        }
    }
    return NO;
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.