Search code examples
iphoneiosobjective-cuiviewuitouch

Event handling in subclass of UIView


As the guide Event Handling Guide for iOS mentions, when you create your own subclass of UIView:

All views that process touches expect to receive a full touch-event stream, so when you create your subclass, keep in mind the following rules:

 - If your custom responder is a subclass of UIView or UIViewController, you should implement all of the event handling methods.

 - If you subclass any other responder class, you can have a null implementation for some of the event methods.

 **- In all methods, be sure to call the superclass implementation of the method.**

However in the "Best Practices for Handling Multitouch Events" part of the guide, it also says:

If you handle events in a subclass of UIView, UIViewController, or UIResponder:

 - Implement all of the event handling methods, even if your implementations of those methods do nothing.

 **- Do not call the superclass implementation of the methods.**

If you handle events in a subclass of any other UIKit responder class:

 - You do not have to implement all of the event handling methods.

 **- In the methods you do implement, be sure to call the superclass implementation. For example, [super touchesBegan:touches withEvent:event].**

Here is my question, should I call the the super class implementation like [super touchesBegan:touches withEvent:event] or not?


Solution

  • If you want to absorb the touch within your subclassed view, then you should 'not' call super touches method. However, if you want to make the view capable of passing the touches to next responder, then you can implement the super touches method. Hope this makes it clear.