iOS 鍵盤的彈出與隱藏主要透過通知來掌握:
// 每個通知都包含一個 nil object 與一個 userInfo 字典,內含鍵盤在螢幕座標下的起始與結束 frame。 // 可用各類 UIView、UIWindow 的 convertRect 方法換算成所需的座標系。 // 動畫相關的 key/value 僅在 "will" 系列通知中提供。 UIKIT_EXTERN NSString *const UIKeyboardWillShowNotification; UIKIT_EXTERN NSString *const UIKeyboardDidShowNotification; UIKIT_EXTERN NSString *const UIKeyboardWillHideNotification; UIKIT_EXTERN NSString *const UIKeyboardDidHideNotification;
具體獲取方式——先註冊觀察:
- (void)keyboardWillShow:(NSNotification *)notification {
// 獲取通知的資訊字典
NSDictionary *userInfo = [notification userInfo];
// 獲取鍵盤彈出後的 rect
NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
// 獲取鍵盤彈出動畫時間
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
// do something...
}
- (void)keyboardWillHide:(NSNotification *)notification {
// 獲取通知資訊字典
NSDictionary* userInfo = [notification userInfo];
// 獲取鍵盤隱藏動畫時間
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
// do something...
}為了避免專案裡每次取得鍵盤彈出/隱藏資訊都要重複註冊觀察,我特別封裝了一個鍵盤通知模組,把註冊通知與取得鍵盤高度、動畫時間的邏輯集中處理。
先看 .h 檔:
@protocol LMJKeyboardShowHiddenNotificationCenterDelegate // height 鍵盤當前高度 // animationDuration 彈出隱藏動畫時間 // isShow 是否是彈出 - (void)showOrHiddenKeyboardWithHeight:(CGFloat)height withDuration:(CGFloat)animationDuration isShow:(BOOL)isShow; @end @interface LMJKeyboardShowHiddenNotificationCenter : NSObject // 這是一個單例,透過該方法獲取單例物件 + (LMJKeyboardShowHiddenNotificationCenter *)defineCenter; // 代理是關鍵設定,若要在當前物件中取得鍵盤彈出隱藏,務必先將單例代理設為該物件 @property (nonatomic, assign) id delegate; // 在使用本模組的物件的 dealloc 中呼叫此函式 - (void)closeCurrentNotification; @end
.m 檔:
@implementation LMJKeyboardShowHiddenNotificationCenter
+ (LMJKeyboardShowHiddenNotificationCenter *)defineCenter {
static LMJKeyboardShowHiddenNotificationCenter * center = nil;
if (center == nil) {
center = [[LMJKeyboardShowHiddenNotificationCenter alloc] init];
center.delegate = nil;
// 添加觀察
[[NSNotificationCenter defaultCenter] addObserver:center selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:center selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
return center;
}
- (void)setDelegate:(id)delegate {
_delegate = delegate;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
// 檢查代理是否為空
if ([self isBlanceObject:self.delegate]) {
return;
}
// 調用代理
if ([self.delegate respondsToSelector:@selector(showOrHiddenKeyboardWithHeight:withDuration:isShow:)]) {
[self.delegate showOrHiddenKeyboardWithHeight:keyboardRect.size.height withDuration:animationDuration isShow:YES];
}
}
- (void)keyboardWillHide:(NSNotification *)notification {
NSDictionary* userInfo = [notification userInfo];
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
if ([self isBlanceObject:self.delegate]) {
return;
}
if ([self.delegate respondsToSelector:@selector(showOrHiddenKeyboardWithHeight:withDuration:isShow:)]) {
[self.delegate showOrHiddenKeyboardWithHeight:0.0 withDuration:animationDuration isShow:NO];
}
}
// 判斷物件是否為空
- (BOOL)isBlanceObject:(id)object {
if (object == nil || object == NULL) {
return YES;
}
if ([object isKindOfClass:[NSNull class]]) {
return YES;
}
return NO;
}
- (void)closeCurrentNotification {
self.delegate = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end演示 Demo 的 GitHub 位址:
https://github.com/MajorLMJ/LMJKeyboardShowHiddenNotificationCenter
文章轉自 MajorLMJ技術博客