繼承樹
UIButton -> UIControl -> UIView -> UIResponder -> NSObject
NSObject
所有 Objective-C 對象的基類首启,封裝了內(nèi)存管理,消息的傳遞機制等底層邏輯撤摸。
UIResponder
UIResponder 定義了響應和處理事件的接口毅桃。它是 UIApplication 和 UIView 的父類,而 UIView 則是 UIWindow 的父類准夷。
有兩種一般事件:
- 點擊事件(Touche Events)
- 手勢事件(Motion Events)
除此之外還有:
- 遠程控制事件(Remote Control Events)
- 重壓事件(Press Events)(iOS 9.0 3D Touch)
點擊 事件的主要處理方法有:
- touchesBegan:withEvent:
- touchesMoved:withEvent:
- touchesEnded:withEvent:
- touchesCancelled:withEvent:.
iOS 3.0 引入了 手勢 事件钥飞,特別是搖一搖。
iOS 4.0 增加了遠程控制事件衫嵌,包括控制中心和耳機線控:
UIView
UIView 定義了一個屏幕上的矩形區(qū)域读宙,以及管理這個區(qū)域內(nèi)容的接口。UIView 提供了一個基本行為就是為這個矩形區(qū)域填充背景色(Background Color)楔绞。
UIControl
UIControl 是 UIButton结闸,UISwitch,UITextField 以及 UISegmentedControl 等類的父類酒朵。
不要使用 UIControl 的實例桦锄,而是寫 UIControl 的子類。
UIControl 子類的主要工作就是將 UIResponder 收集到的復雜事件蔫耽,變成簡單的控制事件(UIControl Events)结耀。而為了實現(xiàn)這個過程,UIControl 引入了 Target-Action 機制针肥。
typedef NS_OPTIONS(NSUInteger, UIControlEvents) {
UIControlEventTouchDown = 1 << 0, // on all touch downs
UIControlEventTouchDownRepeat = 1 << 1, // on multiple touchdowns (tap count > 1)
UIControlEventTouchDragInside = 1 << 2,
UIControlEventTouchDragOutside = 1 << 3,
UIControlEventTouchDragEnter = 1 << 4,
UIControlEventTouchDragExit = 1 << 5,
UIControlEventTouchUpInside = 1 << 6,
UIControlEventTouchUpOutside = 1 << 7,
UIControlEventTouchCancel = 1 << 8,
UIControlEventValueChanged = 1 << 12, // sliders, etc.
UIControlEventPrimaryActionTriggered NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 13, // semantic action: for buttons, etc.
UIControlEventEditingDidBegin = 1 << 16, // UITextField
UIControlEventEditingChanged = 1 << 17,
UIControlEventEditingDidEnd = 1 << 18,
UIControlEventEditingDidEndOnExit = 1 << 19, // 'return key' ending editing
UIControlEventAllTouchEvents = 0x00000FFF, // for touch events
UIControlEventAllEditingEvents = 0x000F0000, // for UITextField
UIControlEventApplicationReserved = 0x0F000000, // range available for application use
UIControlEventSystemReserved = 0xF0000000, // range reserved for internal framework use
UIControlEventAllEvents = 0xFFFFFFFF
};
此外饼记,UIControl 還定義了狀態(tài) UIControlState香伴。
typedef NS_OPTIONS(NSUInteger, UIControlState) {
UIControlStateNormal = 0,
UIControlStateHighlighted = 1 << 0, // used when UIControl isHighlighted is set
UIControlStateDisabled = 1 << 1,
UIControlStateSelected = 1 << 2, // flag usable by app (see below)
UIControlStateFocused NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 3, // Applicable only when the screen supports focus
UIControlStateApplication = 0x00FF0000, // additional flags available for application use
UIControlStateReserved = 0xFF000000 // flags reserved for internal framework use
};
UIButton
UIButton 將 UIResponder 接受的 Events 處理成簡單事件慰枕。