监听键盘事件

注册监听键盘事件:

 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 name:UIKeyboardWillShowNotification object:nil];
 5 
 6     [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
 7 
 8     [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
 9 
10     [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
11 }

 

可以获取的userInfo属性值:

关键字 描述
UIKeyboardAnimationCurveUserInfoKey  动画曲线类型(UIViewAnimationCurve),数值是NSNumber
UIKeyboardAnimationDurationUserInfoKey  动画持续时间,数值是NSNumber
UIKeyboardBoundsUserInfoKey  键盘的大小
UIKeyboardCenterBeginUserInfoKey  键盘开始的中心点
UIKeyboardCenterEndUserInfoKey  键盘结束的中心点
UIKeyboardFrameBeginUserInfoKey  动画前键盘的位置,包含CGRect的NSValue
UIKeyboardFrameChangedByUserInteraction  改变键盘的交互状态
UIKeyboardFrameEndUserInfoKey  动画结束后的键盘位置,包含CGRect的NSValue
posted @ 2015-07-16 00:44  jay_zhang  阅读(891)  评论(0编辑  收藏  举报