iOS键盘弹出高度以及动画时间获取
2016-09-26 11:18
676 查看
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
文章转自 MajorLMJ技术博客
// 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
文章转自 MajorLMJ技术博客
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用开发中AFNetworking库的常用HTTP操作方法小结
- iOS应用中UISearchDisplayController搜索效果的用法
- iOS App开发中的UISegmentedControl分段组件用法总结
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- iOS中UIAlertView警告框组件的使用教程
- 浅析iOS应用开发中线程间的通信与线程安全问题
- iOS中的UIKeyboard键盘视图使用方法小结
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法