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