iOS鍵盤彈出隱藏主要通過通知獲取
// Each notification includes a nil object and a userInfo dictionary containing the
// begining and ending keyboard frame in screen coordinates. Use the various UIView and
// UIWindow convertRect facilities to get the frame in the desired coordinate system.
// Animation key/value pairs are only available for the "will" family of notification.
UIKIT_EXTERN NSString *const UIKeyboardWillShowNotification __TVOS_PROHIBITED;
UIKIT_EXTERN NSString *const UIKeyboardDidShowNotification __TVOS_PROHIBITED;
UIKIT_EXTERN NSString *const UIKeyboardWillHideNotification __TVOS_PROHIBITED;
UIKIT_EXTERN NSString *const UIKeyboardDidHideNotification __TVOS_PROHIBITED;
具體獲取方法:
添加觀察
- (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;
// 在使用LMJKeyboardShowHiddenNotificationCenter的對象的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];
// 獲取鍵盤彈出后的rect
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