iOS開發UIResponder簡介API

#import <Foundation/Foundation.h>
#import <UIKit/UIKitDefines.h>
#import <UIKit/UIEvent.h>
#import <UIKit/UIPasteConfigurationSupporting.h>

NS_ASSUME_NONNULL_BEGIN

@class UIPress;
@class UIPressesEvent;
//響應者類的按鍵命令類類目
@protocol UIResponderStandardEditActions <NSObject>
@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>
//響應鏈中負責傳遞事件的方法
#if UIKIT_DEFINE_AS_PROPERTIES
@property(nonatomic, readonly, nullable) UIResponder *nextResponder;
#else
- (nullable UIResponder*)nextResponder;
#endif

//一個響應對象是否能夠成爲第一響應者,能夠用這個進行判斷,默認值爲NO
#if UIKIT_DEFINE_AS_PROPERTIES
@property(nonatomic, readonly) BOOL canBecomeFirstResponder;    // default is NO
#else
- (BOOL)canBecomeFirstResponder;    // default is NO
#endif
//設置對象成爲第一響應者,成功返回YES;不然返回NO
- (BOOL)becomeFirstResponder;

//是否能夠辭去第一響應者,默認值爲YES
#if UIKIT_DEFINE_AS_PROPERTIES
@property(nonatomic, readonly) BOOL canResignFirstResponder;    // default is YES
#else
- (BOOL)canResignFirstResponder;    // default is YES
#endif
//辭去第一響應者 成功返回YES;不然返回NO
- (BOOL)resignFirstResponder;

//斷定一個響應對象是不是第一響應者
#if UIKIT_DEFINE_AS_PROPERTIES
@property(nonatomic, readonly) BOOL isFirstResponder;
#else
- (BOOL)isFirstResponder;
#endif

//響應觸摸事件
- (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;//取消(非正常離開屏幕,意外中斷)
- (void)touchesEstimatedPropertiesUpdated:(NSSet<UITouch *> *)touches NS_AVAILABLE_IOS(9_1);// Apple Pencil 產生的 touch 事件的部分信息(如 Pencil 的方向等)傳遞到 iPad 或 iPhone 上會有必定的延時。
//UIKit 的回調方法 touchBegan 是當即產生的,其返回的參數 touch 中包含了 Pencil 產生的額外信息,這個額外信息是有延時的。因此,首次回調時會給出額外信息的預估值,延時獲取真實值以後會調用 touchesEstimatedPropertiesUpdated 方法更新額外信息。

//物理按鈕 深按API,通常用於遙控器
- (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);
////默認的實現是調用canPerformAction:withSender:方法來肯定對象是否能夠調用action操做。若是咱們想要重寫目標的選擇方式,則應該重寫這個方法。
- (nullable id)targetForAction:(SEL)action withSender:(nullable id)sender NS_AVAILABLE_IOS(7_0);

//UIResponder提供了一個只讀方法來獲取響應鏈中共享的undo管理器,公共的事件撤銷管理者
@property(nullable, nonatomic,readonly) NSUndoManager *undoManager NS_AVAILABLE_IOS(3_0);

@end

//定義一個響應者支持的快捷鍵
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 (nullable,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);
//彈出鍵盤時附帶的視圖
@property (nullable, nonatomic, readonly, strong) __kindof UIView *inputAccessoryView NS_AVAILABLE_IOS(3_2);

/// This method is for clients that wish to put buttons on the Shortcuts Bar, shown on top of the keyboard.
/// You may modify the returned inputAssistantItem to add to or replace the existing items on the bar.
/// Modifications made to the returned UITextInputAssistantItem are reflected automatically.
/// This method should not be overriden. Goes up the responder chain.
@property (nonnull, nonatomic, readonly, strong) UITextInputAssistantItem *inputAssistantItem NS_AVAILABLE_IOS(9_0) __TVOS_PROHIBITED __WATCHOS_PROHIBITED;

//鍵盤輸入視圖控制器 IOS8之後
@property (nullable, nonatomic, readonly, strong) UIInputViewController *inputViewController NS_AVAILABLE_IOS(8_0);
//彈出鍵盤時附帶的視圖的視圖控制器 IOS8之後
@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);

//從新刷新鍵盤輸入視圖
- (void)reloadInputViews NS_AVAILABLE_IOS(3_2);

@end

// 按鍵輸入箭頭指向
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

#if TARGET_OS_IOS
@interface UIResponder (UIPasteConfigurationSupporting) <UIPasteConfigurationSupporting>
@end
#endif

NS_ASSUME_NONNULL_END
相關文章
相關標籤/搜索