CustomButton
, a subclass of UIButton, is embedded in CustomView
, which is a UIView subclass.
Tap events on CustomButton
do not bubble to CustomView
, because per the Apple docs:
In iOS 6.0 and later, default control actions prevent overlapping gesture recognizer behavior. For example, the default action for a button is a single tap. If you have a single tap gesture recognizer attached to a button’s parent view, and the user taps the button, then the button’s action method receives the touch event instead of the gesture recognizer.
and:
If you have a custom subclass of one of these controls and you want to change the default action, attach a gesture recognizer directly to the control instead of to the parent view. Then, the gesture recognizer receives the touch event first. As always, be sure to read the iOS Human Interface Guidelines to ensure that your app offers an intuitive user experience, especially when overriding the default behavior of a standard control.
1) But if you attach a new tap gesture recognizer, doesn't this create two recognizers: the default and your custom one?
2) How do you override the default one and leverage its native functionality (e.g., highlighting button on touch) instead of creating an independent recognizer?
3) What's the right way to override the tap gesture for a UIButton subclass such that tap events bubble to the superview?
Most appreciated if examples were given in Swift.