iOS-UIKit框架学习—UIControl 发表于 2017-03-08 | 分类于 iOS | | 阅读次数: UIControl是为控制对象,如用户的意图传达到应用程序的按钮和滑块的基类。你不能直接使用UIControl类实例化控制。相反,它定义了共同的界面和它的所有子类的行为结构。 1234567891011121314151617181920212223242526272829303132333435363738394041424344NS_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 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051typedef 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 // 保留};