Pwner
Pwner

Reputation: 3785

How do I enable user interaction on child elements but ignore user interaction on the parent?

In the following diagram, S, P, C's are all subclasses of UIView. C's are children of P. P is a child of S. I want S and C's to respond to touches, but I want P to ignore all touches and propagate them to S.

 _____________________
| S                  |
|  ______________    |
| | P            |   |
| |              |   |
| |  ___         |   |
| | |   |        |   |
| | | C |        |   |
| | |___|   ___  |   |
| |        |   | |   |
| |        | C | |   |
| |        |___| |   |
| |______________|   |
|____________________|

I can't just do p.userInteractionEnabled = NO because that would disable C's as well.

If you are wondering why I need to do this, I want to have a structure similar to Facebook chat heads. The avatars, close button, and comment blurbs can be my C's. They can all be wrapped in a P, which has a transparent background and takes up the whole screen. P is the main view of a UIViewController that only handles chatting logic. Let's say the user tries to tap the Like button (owned by S). Then P should not block that tap - it should let S handle it.

enter image description here

Upvotes: 2

Views: 1413

Answers (2)

Sam V.
Sam V.

Reputation: 411

I would try cutting P out of the picture all together, I'm not sure what purpose it's serving beyond being a container. If you're looking to manage multiple Cs simultaneously, try putting them into an array, or an IBOutletCollection (if they're IBOutlets). You generally don't want two viewControllers on screen, with the same frame, both visible at the same time (pretty much exactly for this reason). You might consider having some object that's a chatModel, which can provide the needed info to S's viewController on demand, and have S directly present Cs.

Upvotes: 0

Ken Kuan
Ken Kuan

Reputation: 798

You can subclass a UIView and override - pointInside: withEvent: like below.

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    CGPoint localPoint = [self convertPoint:point fromView:self];
    for (UIView *subview in self.subviews) {
        if ([subview pointInside:localPoint withEvent:event]) {
            return YES;
        }
    }
    return NO;
}

Upvotes: 3

Related Questions