TouchUpInside on UIView with children
Asked Answered
K

5

24

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?

Ketene answered 23/5, 2012 at 22:36 Comment(0)
G
56

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

Gunter answered 24/5, 2012 at 7:36 Comment(1)
@WebOrCode: I've edited my answer and added screenshots, hope it helps.Gunter
I
8

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.

Indiscernible answered 23/5, 2012 at 23:24 Comment(0)
M
3

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!")
    }
Merchantable answered 13/4, 2017 at 14:19 Comment(0)
M
1

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.

Mourning answered 23/5, 2012 at 23:24 Comment(0)
F
1

Swift 4:

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

 @objc func onUiViewClick(sender : UITapGestureRecognizer) {

    }
Farmer answered 16/2, 2019 at 0:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.