2013-05-04 98 views
3

由於Event Handling Guide for iOS提到導向的子類,當你創建你自己的UIView子類:事件處理中的UIView

是處理觸摸期望得到一個完整的觸摸事件流,所有的意見,所以當你創建你的子類,牢記以下規則:

- 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.** 

然而,在「最佳實踐處理多點觸控事件」導向的一部分,它也說:

如果你的UIView,UIViewController中的一個子類處理事件,或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.** 

如果處理事件在任何其它的UIKit響應者類的子類:

- 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].** 

這裏是我的問題,我應該調用超類實現像[super touchesBegan:touches withEvent:event]與否?

回答

2

如果你想吸收你的subclassed視圖中的觸摸,那麼你應該'不'調用超級觸摸方法。但是,如果要使視圖能夠將觸摸傳遞給下一個響應者,則可以實現超級觸摸方法。希望這個清楚。

+0

不錯的答案。官方指南讓我感到困惑。 – hrchen 2013-05-04 18:00:05

+0

好的一個,當我們調用超類事件時的任何想法,而不是它將如何映射下一個響應者。 – subramani 2015-02-12 13:22:44

+0

爲例,我添加了子視圖uiview添加viewviewcontrol的子視圖,如果我在子視圖事件處理程序中添加touchesBegan:touches withEvent:event],而不是它如何映射到視圖控制器的下一個響應者。 – subramani 2015-02-12 13:28:38