在iOS中UIResponder类是专门用来响应用户的操作处理各种事件的,包括触摸事件(Touch Events)、运动事件(Motion Events)、远程控制事件(Remote Control Events,如插入耳机调节音量触发的事件)。我们知道UIApplication、UIView、UIViewController这几个类是直接继承自UIResponder,UIWindow是直接继承自UIView的一个特殊的View,所以这些类都可以响应事件。当然我们自定义的继承自UIView的View以及自定义的继承自UIViewController的控制器都可以响应事件。iOS里面通常将这些能响应事件的对象称之为响应者
@protocol UIResponderStandardEditActions
@optional
// 剪切、拷贝、粘贴、选择、全选、删除事件
- (void)cut:(nullable id)sender NS_AVAILABLE_IOS(3_0);
- (void)copy:(nullable id)sender NS_AVAILABLE_IOS(3_0);
- (void)paste:(nullable id)sender NS_AVAILABLE_IOS(3_0);
- (void)select:(nullable id)sender NS_AVAILABLE_IOS(3_0);
- (void)selectAll:(nullable id)sender NS_AVAILABLE_IOS(3_0);
- (void)delete:(nullable id)sender NS_AVAILABLE_IOS(3_2);
// 从左到右写入字符串(居左)
- (void)makeTextWritingDirectionLeftToRight:(nullable id)sender NS_AVAILABLE_IOS(5_0);
// 从右到左写入字符串(居右)
- (void)makeTextWritingDirectionRightToLeft:(nullable id)sender NS_AVAILABLE_IOS(5_0);
// 切换字体为黑体(粗体)
- (void)toggleBoldface:(nullable id)sender NS_AVAILABLE_IOS(6_0);
// 切换字体为斜体
- (void)toggleItalics:(nullable id)sender NS_AVAILABLE_IOS(6_0);
// 给文字添加下划线
- (void)toggleUnderline:(nullable id)sender NS_AVAILABLE_IOS(6_0);
// 增加字体大小
- (void)increaseSize:(nullable id)sender NS_AVAILABLE_IOS(7_0);
// 减小字体大小
- (void)decreaseSize:(nullable id)sender NS_AVAILABLE_IOS(7_0);
@end
NS_CLASS_AVAILABLE_IOS(2_0) @interface UIResponder : NSObject <UIResponderStandardEditActions>
// 返回接收者的下一个相应
@property(nonatomic, readonly, nullable) UIResponder *nextResponder;
// 判断一个对象是否可以成为第一响应者。默认返回NO
@property(nonatomic, readonly) BOOL canBecomeFirstResponder; // default is NO
// 接收者接受了第一响应者的状态就返回YES,拒绝了这个状态就返回NO。默认返回YES
- (BOOL)becomeFirstResponder;
// 对象是否可以放弃第一响应者。默认返回YES
@property(nonatomic, readonly) BOOL canResignFirstResponder; // default is YES
// 是否放弃第一响应者
- (BOOL)resignFirstResponder;
// 判断一个对象是否是第一响应者
@property(nonatomic, readonly) BOOL isFirstResponder;
// 通知接收者有触摸事件开始 (手指开始接触屏幕)
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
// 通知接收者有触摸事件位置移动 (手指移动)
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
// 通知接收者有触摸事件结束 (手指离开屏幕)
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
// 通知接收者意外中断触摸事件 (电话打扰)
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
// 通知接收者3D触摸事件
- (void)touchesEstimatedPropertiesUpdated:(NSSet<UITouch *> *)touches NS_AVAILABLE_IOS(9_1);
// 按压事件开始
- (void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);
// 按压事件的位置移动
- (void)pressesChanged:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);
// 结束按压事件
- (void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);
// 按压事件中断
- (void)pressesCancelled:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);
// 开始加速
- (void)motionBegan:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);
// 结束加速
- (void)motionEnded:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);
// 加速中断
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);
// 远程控制事件
- (void)remoteControlReceivedWithEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(4_0);
// 可以执行的事件 (UIMenuController的操作事件)
- (BOOL)canPerformAction:(SEL)action withSender:(nullable id)sender NS_AVAILABLE_IOS(3_0);
// 返回响应的操作目标对象
- (nullable id)targetForAction:(SEL)action withSender:(nullable id)sender NS_AVAILABLE_IOS(7_0);
// 返回响应链就近共享撤消管理
@property(nullable, nonatomic,readonly) NSUndoManager *undoManager NS_AVAILABLE_IOS(3_0);
//响应者支持的快捷键
typedef NS_OPTIONS(NSInteger,UIKeyModifierFlags) {
UIKeyModifierAlphaShift = 1 <<16, // Alppha+Shift键
UIKeyModifierShift = 1 <<17, //Shift键
UIKeyModifierControl = 1 <<18, //Control键
UIKeyModifierAlternate = 1 <<19, //Alt键
UIKeyModifierCommand = 1 <<20, //Command键
UIKeyModifierNumericPad = 1 <<21, //Num键
}NS_ENUM_AVAILABLE_IOS(7_0);
NS_CLASS_AVAILABLE_IOS(7_0) @interface UIKeyCommand : NSObject <NSCopying, NSSecureCoding>
- (instancetype)init NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
// 输入字符串
@property (nonatomic,readonly) NSString *input;
// 按键调节器
@property (nonatomic,readonly) UIKeyModifierFlags modifierFlags;
// 按指定调节器键输入字符串并设置事件
@property (nullable,nonatomic,copy) NSString *discoverabilityTitle NS_AVAILABLE_IOS(9_0);
// The action for UIKeyCommands should accept a single (id)sender, as do the UIResponderStandardEditActions above
// Creates an key command that will _not_ be discoverable in the UI.
+ (UIKeyCommand *)keyCommandWithInput:(NSString *)input modifierFlags:(UIKeyModifierFlags)modifierFlags action:(SEL)action;
// Key Commands with a discoverabilityTitle _will_ be discoverable in the UI.
+ (UIKeyCommand *)keyCommandWithInput:(NSString *)input modifierFlags:(UIKeyModifierFlags)modifierFlags action:(SEL)action discoverabilityTitle:(NSString *)discoverabilityTitle NS_AVAILABLE_IOS(9_0);
@end
@interface UIResponder (UIResponderKeyCommands)
// 组合快捷键命令(装有多个按键的数组)
@property (nullable,nonatomic,readonly) NSArray<UIKeyCommand *> *keyCommands NS_AVAILABLE_IOS(7_0); // returns an array of UIKeyCommand objects<
@end
@class UIInputViewController;
@class UITextInputMode;
@class UITextInputAssistantItem;
@interface UIResponder (UIResponderInputViewAdditions)
// 键盘输入视图(系统默认的,可以自定义)
@property (nullable, nonatomic, readonly, strong) __kindof UIView *inputView NS_AVAILABLE_IOS(3_2);
// 弹出键盘时附带的视图 第一响应者的附件View,在becomeFirstResponder时会在键盘的顶端显示自定义的inputAccessoryView
@property (nullable, nonatomic, readonly, strong) __kindof UIView *inputAccessoryView NS_AVAILABLE_IOS(3_2);
// 输入助手配置键盘的快捷方式栏时使用
@property (nonnull, nonatomic, readonly, strong) UITextInputAssistantItem *inputAssistantItem NS_AVAILABLE_IOS(9_0) __TVOS_PROHIBITED __WATCHOS_PROHIBITED;
// 键盘输入视图控制器
@property (nullable, nonatomic, readonly, strong) UIInputViewController *inputViewController NS_AVAILABLE_IOS(8_0);
// 弹出键盘时附带的视图的视图控制器
@property (nullable, nonatomic, readonly, strong) UIInputViewController *inputAccessoryViewController NS_AVAILABLE_IOS(8_0);
// 文本输入响应的对象
@property (nullable, nonatomic, readonly, strong) UITextInputMode *textInputMode NS_AVAILABLE_IOS(7_0);
// 标识符表示该响应应保留其文本输入方式的信息
@property (nullable, nonatomic, readonly, strong) NSString *textInputContextIdentifier NS_AVAILABLE_IOS(7_0);
// 清除从应用程序的用户默认文本输入模式的信息
+ (void)clearTextInputContextIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(7_0);
// 如果成为第一响应者,将会重新加载inputView、inputAccesoryView和textInputMode,否则忽略
- (void)reloadInputViews NS_AVAILABLE_IOS(3_2);
@end
// 这是一些预定义的UIKeyCommand对象使用的输入属性,按键输入箭头指向
UIKIT_EXTERN NSString *const UIKeyInputUpArrow NS_AVAILABLE_IOS(7_0);
UIKIT_EXTERN NSString *const UIKeyInputDownArrow NS_AVAILABLE_IOS(7_0);
UIKIT_EXTERN NSString *const UIKeyInputLeftArrow NS_AVAILABLE_IOS(7_0);
UIKIT_EXTERN NSString *const UIKeyInputRightArrow NS_AVAILABLE_IOS(7_0);
UIKIT_EXTERN NSString *const UIKeyInputEscape NS_AVAILABLE_IOS(7_0);
@interface UIResponder (ActivityContinuation)
// 用户活动
@property (nullable, nonatomic, strong) NSUserActivity *userActivity NS_AVAILABLE_IOS(8_0);
// 更新用户活动
- (void)updateUserActivityState:(NSUserActivity *)activity NS_AVAILABLE_IOS(8_0);
// 恢复用户活动
- (void)restoreUserActivityState:(NSUserActivity *)activity NS_AVAILABLE_IOS(8_0);
@end
网友评论