TouchUpInside on UIView with children

14.9k views Asked by At

I may be missing something here, but...

I have a UIView with a few children (a couple of UILabel's and UIImageView). I need to catch an event when the user clicks (taps) anywhere within the parent UIView - visible part of the UIView or any of its children).

What's the best way of doing this?

5

There are 5 answers

1
DarkByte On BEST ANSWER

You can change the UIView's class in the Identity Inspector ... make it UIControl and then you can add an event for UITouchUpInside for your view - to catch the taps on it.

Good luck!

EDIT: here's a step-by-step with screenshots:

The view... Original view - of type UIView

... of course, no events. UIView has no events you can use

Go back and change UIView with UIControl and... Change UIView to UIControl

Ta-daa! Events! UIControl allows you to use events

Hook a method to the UITouchUpInside event Add event for UITouchUpInside

This is how it should look like in the interface file The interface of the method

This is how it should look like in the implementation file (you can, of course, replace the logging with anything you want) The implementation of the method

0
alinoz On

you have to give a bit more details because there are multiple options.

If you want the normal tap you can catch one of the standard touch events and see what subview got the event. Something like this:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    if(touch.view != self.mySubViewOfInterest)
    {
        // perform my actions
    }
}

If you need complex gesture recognition then you can use one of the UIGestureRecognizer and assign it to the subviews or main view.

Anyway you have to pay attention that all the subviews (the ones you want them to catch the event) must have the user interaction enabled otherwise your main view will capture the events for the subviews too and you will end up debugging and debugging without understanding what is happening.

0
Barjavel On

You can feign this behaviour by having a gesture recogniser for your parent view and ensuring that your children do not have their own gesture recognisers. To do this add a gesture recogniser to your parent UIView that listens for taps. Where parentView is the containing view for your child views (UILabels/UIImageView) and handleTap is a method implemented by parentView, do something like:

UIGestureRecognizer *tapParent = [[UITapGestureRecognizer alloc]initWithTarget:parentView action:@selector(handleTap)];
[parentView addGestureRecognizer:tapParent];
[tapParent release];

As your child views do not have their own gesture recognisers they will not 'eat' the gesture and so the parent view will respond so long as you tap within its bounds.

0
Oz Shabat On

Swift 4:

let clickGesture = UITapGestureRecognizer(target: self, action:  #selector(self.onUiViewClick))
uiView.addGestureRecognizer(clickGesture)

 @objc func onUiViewClick(sender : UITapGestureRecognizer) {

    }
0
Sumit On

This works for me

Swift 3

 let gesture = UITapGestureRecognizer(target: self, action: selector(self.customViewClick))
withSender: self)
 customView.addGestureRecognizer(gesture)


 func customViewClick()  {
     print("custom view clicked!")
    }