圖片源于網(wǎng)絡(luò)
鍵盤通知的基本信息
當(dāng)鍵盤彈出時槐雾,鍵盤高度會隨著鍵盤語言變化(中文要高些),在這種情況下一般而言對于界面需要重新布局。這就需要用到鍵盤通知(NSNotificationCenter)。
- 鍵盤通知目前的SDK里總共有如下6個:
UIKIT_EXTERN NSNotificationName const UIKeyboardWillShowNotification __TVOS_PROHIBITED;
UIKIT_EXTERN NSNotificationName const UIKeyboardDidShowNotification __TVOS_PROHIBITED;
UIKIT_EXTERN NSNotificationName const UIKeyboardWillHideNotification __TVOS_PROHIBITED;
UIKIT_EXTERN NSNotificationName const UIKeyboardDidHideNotification __TVOS_PROHIBITED;
// Like the standard keyboard notifications above, these additional notifications include
// a nil object and begin/end frames of the keyboard in screen coordinates in the userInfo dictionary.
UIKIT_EXTERN NSNotificationName const UIKeyboardWillChangeFrameNotification NS_AVAILABLE_IOS(5_0) __TVOS_PROHIBITED;
UIKIT_EXTERN NSNotificationName const UIKeyboardDidChangeFrameNotification NS_AVAILABLE_IOS(5_0) __TVOS_PROHIBITED;
通知是一種消息機制,addObserver與removeObserver需要在對應(yīng)的生命周期中成對出現(xiàn)。
在鍵盤通知(NSNotificationCenter)的userInfo字典中包含了一些鍵盤frame以及動畫相關(guān)的信息:
UIKIT_EXTERN NSString *const UIKeyboardFrameBeginUserInfoKey NS_AVAILABLE_IOS(3_2) __TVOS_PROHIBITED; // NSValue of CGRect
UIKIT_EXTERN NSString *const UIKeyboardFrameEndUserInfoKey NS_AVAILABLE_IOS(3_2) __TVOS_PROHIBITED; // NSValue of CGRect
UIKIT_EXTERN NSString *const UIKeyboardAnimationDurationUserInfoKey NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED; // NSNumber of double
UIKIT_EXTERN NSString *const UIKeyboardAnimationCurveUserInfoKey NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED; // NSNumber of NSUInteger (UIViewAnimationCurve)
UIKIT_EXTERN NSString *const UIKeyboardIsLocalUserInfoKey NS_AVAILABLE_IOS(9_0) __TVOS_PROHIBITED; // NSNumber of BOOL
// These keys are superseded by UIKeyboardFrameBeginUserInfoKey and UIKeyboardFrameEndUserInfoKey.
UIKIT_EXTERN NSString *const UIKeyboardCenterBeginUserInfoKey NS_DEPRECATED_IOS(2_0, 3_2) __TVOS_PROHIBITED;
UIKIT_EXTERN NSString *const UIKeyboardCenterEndUserInfoKey NS_DEPRECATED_IOS(2_0, 3_2) __TVOS_PROHIBITED;
UIKIT_EXTERN NSString *const UIKeyboardBoundsUserInfoKey NS_DEPRECATED_IOS(2_0, 3_2) __TVOS_PROHIBITED;
- NSValue: NSValue提供了簡單的容器來包含C或Objective-C數(shù)據(jù)項北专。可以容納任何基本數(shù)據(jù)類型如char旬陡,int拓颓,float,double描孟,以及指針驶睦,結(jié)構(gòu)體和對象ids。NSArray和NSSet集合類對象要求它們的元素為對象類型画拾,NSValue的主要目的是使這些數(shù)據(jù)類型可以添加至集合中啥繁。NSValue對象是不可變類型。 簡而言之青抛,NSValue是基本數(shù)據(jù)類型或自定義數(shù)據(jù)類型所定義變量的對象包裝器旗闽。
注:NSNumber等都是繼承于NSValue。(通過NSValueUIGeometryExtensions這個類別封裝CGPoint,CGSize,CGRect等結(jié)構(gòu)體)
@interface NSValue (NSValueUIGeometryExtensions)
+ (NSValue *)valueWithCGPoint:(CGPoint)point;
+ (NSValue *)valueWithCGSize:(CGSize)size;
+ (NSValue *)valueWithCGRect:(CGRect)rect;
+ (NSValue *)valueWithCGAffineTransform:(CGAffineTransform)transform;
+ (NSValue *)valueWithUIEdgeInsets:(UIEdgeInsets)insets;
+ (NSValue *)valueWithUIOffset:(UIOffset)insets NS_AVAILABLE_IOS(5_0);
- (CGPoint)CGPointValue;
- (CGSize)CGSizeValue;
- (CGRect)CGRectValue;
- (CGAffineTransform)CGAffineTransformValue;
- (UIEdgeInsets)UIEdgeInsetsValue;
- (UIOffset)UIOffsetValue NS_AVAILABLE_IOS(5_0);
@end
- UIKeyboardFrameBeginUserInfoKey: 動畫前鍵盤的位置蜜另,包含CGRect的NSValue
- UIKeyboardFrameEndUserInfoKey:動畫結(jié)束后的鍵盤位置适室,包含CGRect的NSValue
NSDictionary* info = [aNotification userInfo];
CGRect rect =[[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
- UIKeyboardAnimationDurationUserInfoKey:動畫持續(xù)時間,數(shù)值是NSNumber
- UIKeyboardAnimationCurveUserInfoKey:動畫曲線類型(UIViewAnimationCurve),數(shù)值是NSNumber
typedef enum {
UIViewAnimationCurveEaseInOut, //淡入淡出,開始時慢举瑰,由慢變快捣辆,中間最快,然后變慢此迅;
UIViewAnimationCurveEaseIn,//淡入汽畴,開始時慢然后越來越快旧巾;
UIViewAnimationCurveEaseOut,//淡出,開始快然后越來越慢忍些;
UIViewAnimationCurveLinear//線性勻速鲁猩,開始和結(jié)束是一個速度。
} UIViewAnimationCurve;
- UIKeyboardIsLocalUserInfoKey 是否是原生鍵盤
- UIKeyboardCenterBeginUserInfoKey 鍵盤開始的時候中心點位置
- UIKeyboardCenterEndUserInfoKey 鍵盤結(jié)束的時候中心點位置
- UIKeyboardBoundsUserInfoKey:鍵盤大小
// These keys are superseded by UIKeyboardFrameBeginUserInfoKey and UIKeyboardFrameEndUserInfoKey.
翻譯:這些鑰匙(UIKeyboardCenterBeginUserInfoKey, UIKeyboardCenterEndUserInfoKey, UIKeyboardBoundsUserInfoKey)被UIKeyboardFrameBeginUserInfoKey和UIKeyboardFrameEndUserInfoKey取代罢坝。
鍵盤通知的使用
- 添加鍵盤監(jiān)聽事件
#pragma mark -添加鍵盤監(jiān)聽事件
/**
添加鍵盤監(jiān)聽事件
*/
-(void)addNotificationForKeyboard
{
// 注冊鍵盤通知
//鍵盤的frame值將要發(fā)生變化
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardWillChangeFrameNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];
//鍵盤的frame值發(fā)生變化
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardDidChangeFrameNotification:) name:UIKeyboardDidChangeFrameNotification object:nil];
// 即將顯示
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
// 顯示
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardDidShowNotification:) name:UIKeyboardDidShowNotification object:nil];
// 即將隱藏
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideNotification:) name:UIKeyboardDidHideNotification object:nil];
// 隱藏
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHideNotification:) name:UIKeyboardDidHideNotification object:nil];
}
- 移除鍵盤監(jiān)聽事件
#pragma mark - 移除鍵盤監(jiān)聽事件
/**
移除鍵盤監(jiān)聽事件
*/
- (void)removeNotificationForKeyboard
{
//鍵盤的frame值將要發(fā)生變化
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
//鍵盤的frame值發(fā)生變化
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidChangeFrameNotification object:nil];
// 即將顯示
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
// 顯示
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
// 即將隱藏
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
// 隱藏
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
}
- 鍵盤監(jiān)聽方法
#pragma mark -鍵盤監(jiān)聽方法
- (void) keyboardWillChangeFrameNotification: (NSNotification *)notif{
NSLog(@"鍵盤的frame值將要發(fā)生變化");
}
- (void) keyboardDidChangeFrameNotification: (NSNotification *)notif{
NSLog(@"鍵盤的frame值已經(jīng)發(fā)生變化");
}
- (void) keyboardWillShowNotification: (NSNotification *)notif{
NSLog(@"鍵盤即將顯示");
}
- (void) keyboardDidShowNotification: (NSNotification *)notif{
NSLog(@"鍵盤顯示");
}
- (void) keyboardWillHideNotification:(NSNotification *)notif{
NSLog(@"鍵盤即將隱藏");
}
- (void) keyboardDidHideNotification:(NSNotification *)notif{
NSLog(@"鍵盤隱藏");
}
- notification 中userInfo的完整信息:
/**
{
UIKeyboardAnimationCurveUserInfoKey = 7;
UIKeyboardAnimationDurationUserInfoKey = "0.25";
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 258}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 796}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 538}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 258}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 409}, {375, 258}}";
UIKeyboardIsLocalUserInfoKey = 1;
}
*/
-
鍵盤通知(NSNotificationCenter)正確使用
- 在界面即將出現(xiàn)的時候注冊鍵盤通知
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; //注冊鍵盤通知 [self addNotificationForKeyboard]; }
- 在頁面即將消失的時候移除通知 移除通知要用通知名對應(yīng)去移除
- (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; //移除鍵盤監(jiān)聽 [self removeNotificationForKeyboard]; }
鍵盤通知的發(fā)出順序
- 當(dāng)一個UITextView或UITextField變成第一焦點時廓握,通知的發(fā)出順序如下:
UIKeyboardWillChangeFrameNotification --> UIKeyboardWillShowNotification --> UIKeyboardDidChangeFrameNotification --> UIKeyboardDidShowNotification
- 當(dāng)一個UITextView或UITextField注銷焦點狀態(tài)時,發(fā)出通知順序如下:
UIKeyboardWillChangeFrameNotification --> UIKeyboardWillHideNotification --> UIKeyboardDidChangeFrameNotification --> UIKeyboardDidHideNotification
- 特別需要注意的是嘁酿,當(dāng)屏幕旋轉(zhuǎn)的時候也會發(fā)出鍵盤通知隙券,并且順序如下:
UIKeyboardWillChangeFrameNotification --> UIKeyboardWillHideNotification --> UIKeyboardDidChangeFrameNotification --> UIKeyboardDidHideNotification --> UIKeyboardWillChangeFrameNotification --> UIKeyboardWillShowNotification --> UIKeyboardDidChangeFrameNotification --> UIKeyboardDidShowNotification
/**
注:其順序是一個UITextView或UITextField注銷焦點狀態(tài)時,發(fā)出鍵盤通知順序和一個UITextView或UITextField變成第一焦點時發(fā)出鍵盤順序組合起來一致闹司,也就是說娱仔,在屏幕旋轉(zhuǎn)期間,雖然我們看起來好像鍵盤沒啥變化游桩,但實際上通知已經(jīng)經(jīng)理了從“消失”到再次“顯示”的路徑了拟枚。
*/