码迷,mamicode.com
首页 > 其他好文 > 详细

监听键盘事件

时间:2015-07-16 02:06:44      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

注册监听键盘事件:

 1     // 键盘即将隐藏
 2     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
 3     
 4     // 键盘已经隐藏
 5     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
 6     
 7     // 键盘即将显示
 8     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
 9     
10     // 键盘已经显示完毕
11     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];

实现监听方法,在监听的方法中做自己的事情:

 1 /**
 2 *  键盘即将隐藏
 3 */
 4 - (void)keyboardWillHide:(NSNotification *)notification
 5 {
 6     NSLog(@"keyboardWillHide --> %@", notification);
 7 
 8     // 获取键盘的动画时间
 9     CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
10     [UIView animateWithDuration:duration animations:^{
11 
12         // 获取键盘的高度
13         CGRect keyboardRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
14         
15         // 更改文本框的y值
16         CGRect fieldRect = self.textField.frame;
17         fieldRect.origin.y += keyboardRect.size.height;
18         self.textField.frame = fieldRect;
19     }];
20 }
21 
22 /**
23 *  键盘已经隐藏完毕
24 */
25 - (void)keyboardDidHide:(NSNotification *)notification
26 {
27     NSLog(@"keyboardDidHide --> %@", notification);
28 }
29 
30 /**
31 *  键盘即将显示
32 */
33 - (void)keyboardWillShow:(NSNotification *)notification
34 {
35     // 获取键盘动画时间
36     CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
37     [UIView animateWithDuration:duration animations:^{
38 
39         // 获取键盘的高度
40         CGRect keyboardRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
41         
42         // 更改文本框的y值
43         CGRect fieldRect = self.textField.frame;
44         fieldRect.origin.y -= keyboardRect.size.height;
45         self.textField.frame = fieldRect;
46     }];
47 }
48 
49 /**
50 *  键盘已经显示完毕
51 */
52 - (void)keyboardDidShow:(NSNotification *)notification
53 {
54     NSLog(@"keyboardDidShow --> %@", notification);
55 }

在控制器即将销毁时,移除键盘监听事件:

1 - (void)dealloc
2 {
3     // 移除键盘监听事件
4     [[NSNotificationCenter defaultCenter] removeObserver:self];
5 }

可以获取的userInfo属性值:

关键字 描述
UIKeyboardAnimationCurveUserInfoKey  动画曲线类型(UIViewAnimationCurve),数值是NSNumber
UIKeyboardAnimationDurationUserInfoKey  动画持续时间,数值是NSNumber
UIKeyboardBoundsUserInfoKey  键盘的大小
UIKeyboardCenterBeginUserInfoKey  键盘开始的中心点
UIKeyboardCenterEndUserInfoKey  键盘结束的中心点
UIKeyboardFrameBeginUserInfoKey  动画前键盘的位置,包含CGRect的NSValue
UIKeyboardFrameChangedByUserInteraction  改变键盘的交互状态
UIKeyboardFrameEndUserInfoKey  动画结束后的键盘位置,包含CGRect的NSValue

 

监听键盘事件

标签:

原文地址:http://www.cnblogs.com/initial-road/p/keyboard_event.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!