Reputation: 57316
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?
Upvotes: 24
Views: 15059
Reputation: 1622
Swift 4:
let clickGesture = UITapGestureRecognizer(target: self, action: #selector(self.onUiViewClick))
uiView.addGestureRecognizer(clickGesture)
@objc func onUiViewClick(sender : UITapGestureRecognizer) {
}
Upvotes: 1
Reputation: 1122
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!")
}
Upvotes: 3
Reputation: 1176
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...
... of course, no events.
Go back and change UIView with UIControl and...
Ta-daa! Events!
Hook a method to the UITouchUpInside event
This is how it should look like in the interface file
This is how it should look like in the implementation file (you can, of course, replace the logging with anything you want)
Upvotes: 56
Reputation: 2822
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.
Upvotes: 1
Reputation: 1636
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.
Upvotes: 8