Listening for and Reacting to Keyboard Notifications(键盘通知)
来源:互联网 发布:淘宝买家退款率从哪看 编辑:程序博客网 时间:2024/04/29 00:07
在输入文本时,键盘弹出,遮挡了视图内容,此时可通过键盘通知来使你的UI组件向上或向移动,或对它们进行重组。
各键盘通知:
UIKeyboardWillShowNotification:包含user-info dictionary
UIKeyboardDidShowNotification
UIKeyboardWillHideNotification:包含user-info dictionary
UIKeyboardDidHideNotification
user-info dictionary:
UIKeyboardAnimationCurveUserInfoKey: 包含NSUInteger类型的NSNumber,封装在NSValue中
UIKeyboardAnimationDurationUserInfoKey:包含double类型的NSNumber,封装在NSValue中
UIKeyboardFrameBeginUserInfoKey:CGRect类型,封装在NSValue中,标识动画开始之前的keyboard的frame,使用前进行坐标转换
UIKeyboardFrameEndUserInfoKey:CGRect类型,封装在NSValue中,标识动画开始之后的keyboard的frame,使用前进行坐标转换
e.g.
@interface ViewController () <UITextFieldDelegate>
//View里包含ScrollView,ScrollView里包含ImageView,TextField
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UITextField *textField; //此textField的delegate是自身ViewController
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
//UITextFieldDelegte:return Button
- (BOOL) textFieldShouldReturn:(UITextField *)paramTextField{
[paramTextField resignFirstResponder];
return YES;
}
- (void) viewWillAppear:(BOOL)paramAnimated{
[super viewWillAppear:paramAnimated];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
//注册通知-键盘弹出
[center addObserver:self selector:@selector(handleKeyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];
//注册通知-键盘隐藏
[center addObserver:self selector:@selector(handleKeyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)paramAnimated{
[super viewWillDisappear:paramAnimated];
//取消注册
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void) handleKeyboardWillShow:(NSNotification *)paramNotification{
NSDictionary *userInfo = paramNotification.userInfo;
//获取键盘弹出的动画持续时间,以便后续我们使用相同的时间进行动画呈现内容
NSValue *animationDurationObject = userInfo[UIKeyboardAnimationDurationUserInfoKey];
//获取键盘弹出结束时的frame,是以屏幕坐标形式表示,因此后续使用需要转换成窗体坐标 convertRect:fromView:
NSValue *keyboardEndRectObject =userInfo[UIKeyboardFrameEndUserInfoKey];
double animationDuration = 0.0;
CGRect keyboardEndRect = CGRectMake(0.0f, 0.0f, 0.0f, 0.0f);
[animationDurationObjectgetValue:&animationDuration];
[keyboardEndRectObjectgetValue:&keyboardEndRect];
UIWindow *window = [UIApplication sharedApplication].keyWindow;
keyboardEndRect = [self.view convertRect:keyboardEndRect fromView:window];
/* 键盘覆盖的部分*/
CGRect intersectionOfKeyboardRectAndWindowRect = CGRectIntersection(self.view.frame, keyboardEndRect);
/* 滚动视图以显示遮挡内容*/
[UIView animateWithDuration:animationDuration animations:^{
self.scrollView.contentInset = UIEdgeInsetsMake(0.0f, //上
0.0f, //左
intersectionOfKeyboardRectAndWindowRect.size.height, //下
0.0f); //右
//滚动视图使标识的rect刚好可见
[self.scrollView scrollRectToVisible:self.textField.frame animated:NO];
}];
}
- (void) handleKeyboardWillHide:(NSNotification *)paramSender{
NSDictionary *userInfo = [paramSender userInfo];
NSValue *animationDurationObject = [userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey];
double animationDuration = 0.0;
[animationDurationObject getValue:&animationDuration];
[UIView animateWithDuration:animationDuration animations:^{
self.scrollView.contentInset = UIEdgeInsetsZero; //0,0,0,0
}];
}
- Listening for and Reacting to Keyboard Notifications(键盘通知)
- 2.29 Listening and Reacting to Keyboard Notifications
- Sending , Listening for and Reacting to Notifications (发送,监听通知)
- 1.30 Listening for Notifications Sent from NSNotificationCenter
- 通知机制 (Notifications)
- Notifications(通知)
- Notifications(通知)
- Notifications (通知、通告)
- Keyboard Shortcuts For Windows and Mac - 键盘快捷键
- 浏览器桌面通知(notifications)
- Reacting to rumors
- notifications 消息通知(本地事件通知)
- 通知(Notifications)
- Notifications 通知
- Android Wear Preview- 创建通知(Creating Notifications for Android Wear)
- How to Implement Push Notifications for Android
- How to Implement Push Notifications for Android
- How to Implement Push Notifications for Android
- 一转眼13年 马踏14间
- 一个PHP的数组工具UTIL
- object-c基本知识
- DAO层、model层、service层的理解
- jquery after append appendTo三个函数的区别
- Listening for and Reacting to Keyboard Notifications(键盘通知)
- 关于csdn一篇文章java c++多态对纠正(红色字体为纠正)
- Hibernate的增删查改
- 在服务端解决分页展示页码
- 一个简单的瀑布流
- ubuntu 10.04/ubuntu 12.04 /ubuntu 13.04 编译android 差异
- Pande Lab
- stack overflow at line 9
- 为什么Linux不需要磁盘碎片整理