UIResponder

在iOS中UIResponder類是專門用來響應用戶的操作處理各種事件的,包括觸摸事件(Touch Events)、運動事件(Motion Events)、遠程控制事件(Remote Control Events,如插入耳機調節(jié)音量觸發(fā)的事件)。我們知道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
// 接收者接受了第一響應者的狀態(tài)就返回YES,拒絕了這個狀態(tài)就返回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);
// 可以執(zhí)行的事件 (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;
// 按鍵調節(jié)器
@property (nonatomic,readonly) UIKeyModifierFlags modifierFlags;
// 按指定調節(jié)器鍵輸入字符串并設置事件
@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)
// 組合快捷鍵命令(裝有多個按鍵的數(shù)組)
@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)

// 鍵盤輸入視圖(系統(tǒng)默認的,可以自定義)
@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

參考:
Wynter_Wang
iOS-UIResponder官方API總結
zeng_zhiming

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容