iOS-UIKit框架学习—UIControl

UIControl是为控制对象,如用户的意图传达到应用程序的按钮和滑块的基类。你不能直接使用UIControl类实例化控制。相反,它定义了共同的界面和它的所有子类的行为结构。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
NS_CLASS_AVAILABLE_IOS(2_0) @interface UIControl : UIView
// 触摸事件是否可用 默认值YES
@property(nonatomic,getter=isEnabled) BOOL enabled;
// 是否选中 默认值NO
@property(nonatomic,getter=isSelected) BOOL selected;
// 是否高亮 默认值NO
@property(nonatomic,getter=isHighlighted) BOOL highlighted;
// 内容垂直对齐方式 默认值中心对齐
@property(nonatomic) UIControlContentVerticalAlignment contentVerticalAlignment;
// 内容水平对齐方式 默认值中心对齐
@property(nonatomic) UIControlContentHorizontalAlignment contentHorizontalAlignment;
// 状态
@property(nonatomic,readonly) UIControlState state;
// 是否跟踪当前触摸事件
@property(nonatomic,readonly,getter=isTracking) BOOL tracking;
// 跟踪是否在当前触摸范围内
@property(nonatomic,readonly,getter=isTouchInside) BOOL touchInside;
// 开始跟踪触摸事件
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(nullable UIEvent *)event;
// 当触摸事件关联控件时调用更新
- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(nullable UIEvent *)event;
// 结束跟踪触摸事件
- (void)endTrackingWithTouch:(nullable UITouch *)touch withEvent:(nullable UIEvent *)event;
// 取消触摸事件
- (void)cancelTrackingWithEvent:(nullable UIEvent *)event;
// 将目标控件和操作方法与该控件相关联
- (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
// 移除目标控件和操作方法
- (void)removeTarget:(nullable id)target action:(nullable SEL)action forControlEvents:(UIControlEvents)controlEvents;
// 返回全部与该控件相关的对象
@property(nonatomic,readonly) NSSet *allTargets;
// 返回与控件相关的操作事件
@property(nonatomic,readonly) UIControlEvents allControlEvents;
// 返回指定对象发生操作时的字符串名称数组
- (nullable NSArray<NSString *> *)actionsForTarget:(nullable id)target forControlEvent:(UIControlEvents)controlEvent;
// 调用指定的操作事件
- (void)sendAction:(SEL)action to:(nullable id)target forEvent:(nullable UIEvent *)event;
// 发送与控件相关的全部事件
- (void)sendActionsForControlEvents:(UIControlEvents)controlEvents;
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
typedef NS_OPTIONS(NSUInteger, UIControlEvents) {
UIControlEventTouchDown = 1 << 0, // 单点触摸按下事件
UIControlEventTouchDownRepeat = 1 << 1, // 多点触摸按下事件 (触摸数 > 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, // 当控件的值发生改变时,发送通知
UIControlEventPrimaryActionTriggered NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 13, // semantic action: for buttons, etc.
UIControlEventEditingDidBegin = 1 << 16, // 当文本控件中开始编辑时发送通知
UIControlEventEditingChanged = 1 << 17, // 当文本控件中的文本被改变时发送通知
UIControlEventEditingDidEnd = 1 << 18, // 当文本控件中编辑结束时发送通知
UIControlEventEditingDidEndOnExit = 1 << 19, // 当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知
UIControlEventAllTouchEvents = 0x00000FFF, // 通知所有触摸事件
UIControlEventAllEditingEvents = 0x000F0000, // 通知所有关于文本编辑的事件
UIControlEventApplicationReserved = 0x0F000000, // 通知应用内所有可用的事件
UIControlEventSystemReserved = 0xF0000000, // 通知系统内可用的事件
UIControlEventAllEvents = 0xFFFFFFFF // 通知所有事件
};
// 内容垂直对齐方式
typedef NS_ENUM(NSInteger, UIControlContentVerticalAlignment) {
UIControlContentVerticalAlignmentCenter = 0,
UIControlContentVerticalAlignmentTop = 1,
UIControlContentVerticalAlignmentBottom = 2,
UIControlContentVerticalAlignmentFill = 3,
};
// 内容水平对齐方式
typedef NS_ENUM(NSInteger, UIControlContentHorizontalAlignment) {
UIControlContentHorizontalAlignmentCenter = 0,
UIControlContentHorizontalAlignmentLeft = 1,
UIControlContentHorizontalAlignmentRight = 2,
UIControlContentHorizontalAlignmentFill = 3,
};
// UIControl状态
typedef NS_OPTIONS(NSUInteger, UIControlState) {
UIControlStateNormal = 0,
UIControlStateHighlighted = 1 << 0, // 高亮状态
UIControlStateDisabled = 1 << 1, // 禁用
UIControlStateSelected = 1 << 2, // 选中
UIControlStateFocused NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 3, // 长按聚焦
UIControlStateApplication = 0x00FF0000, // 应用状态可用
UIControlStateReserved = 0xFF000000 // 保留
};