TouchUpInside on UIView with children
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)
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!")
}
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.