Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore touch events and pass them to another subview's UIControl objects?

I have a custom UIViewController whose UIView takes up a corner of the screen, but most of it is transparent except for the parts of it that have some buttons and stuff on it. Due to the layout of the objects on that view, the view's frame can cover up some buttons beneath it. I want to be able to ignore any touches on that view if they aren't touching anything important on it, but I seem to only be able to pass along actual touch events (touchesEnded/nextResponder stuff). If I have a UIButton or something like that which doesnt use touchesEnded, how do I pass the touch event along to that?

I can't just manually figure out button selector to call, because this custom ViewController can be used on many different views. I basically need a way to call this:

[self.nextResponder touchesEnded:touches withEvent:event];

on UIControl types as well.

like image 445
Chris C Avatar asked Oct 10 '11 22:10

Chris C


3 Answers

Probably the best way to do this is to override hitTest:withEvent: in the view that you want to be ignoring touches. Depending on the complexity of your view hierarchy, there are a couple of easy ways to do this.

If you have a reference to the view underneath the view to ignore:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *hitView = [super hitTest:point withEvent:event];

    // If the hitView is THIS view, return the view that you want to receive the touch instead:
    if (hitView == self) {
        return otherView;
    }
    // Else return the hitView (as it could be one of this view's buttons):
    return hitView;
}

If you don't have a reference to the view:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *hitView = [super hitTest:point withEvent:event];

    // If the hitView is THIS view, return nil and allow hitTest:withEvent: to
    // continue traversing the hierarchy to find the underlying view.
    if (hitView == self) {
        return nil;
    }
    // Else return the hitView (as it could be one of this view's buttons):
    return hitView;
}

I would recommend the first approach as being the most robust (if it's possible to obtain a reference to the underlying view).

like image 142
Stuart Avatar answered Nov 03 '22 18:11

Stuart


Swift answer:

You can create a UIView subclass, IgnoreTouchView. In a storyboard, set it on the VC's view(s) you want to pass through touches:

class IgnoreTouchView : UIView {
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let hitView = super.hitTest(point, with: event)
        if hitView == self {
            return nil
        }
        return hitView
    }
}

Note that you must set UserInteractionEnabled to true, for the UIView in question!

like image 18
Kevin Avatar answered Nov 03 '22 17:11

Kevin


You could also disable user interaction so that touch events are ignored and are passed to the parent.

In Swift:

yourView.isUserInteractionEnabled = false
like image 18
Melkon Avatar answered Nov 03 '22 17:11

Melkon