Jordan Smith
Jordan Smith

Reputation: 10378

How can I pass a touch to a button underneath another view?

Pretty simple question, perhaps not so simple answer though:

I've got a clear view which needs to receive touches. Underneath this is a UIButton, which I also want to receive touches (for reasons I won't go into, it has to be underneath). In the case where the button is pressed, I don't want the clear view to receive the touches.

How can I do this?

EDIT:

Final Solution:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {


    for (UIView * view in self.subviews)
    {
        if ([view isKindOfClass:[UIButton class]]) {
            CGPoint pointInButton = [view convertPoint:point fromView:self];
            if ([view pointInside:pointInButton withEvent:event]) {
                return view;
            }
        }
    }
    return [super hitTest:point withEvent:event];
}

Upvotes: 1

Views: 895

Answers (1)

rob mayoff
rob mayoff

Reputation: 385500

Give the clear view a reference to the UIButton. Override the clear view's pointInside:withEvent: method. In your override, check whether the point is inside the button (by sending pointInside:withEvent: to the button). If the point is in the button, return NO. If the point is outside the button, return [super pointInside:point withEvent:event].

Upvotes: 2

Related Questions