先看效果:
首先是我們在ViewDidLoada()中註冊了兩個通知,[NSNotificationCenterdefaultCenter],檢測鍵盤動態,一個是鍵盤將要彈出的時候,另一個是鍵盤將要退出時候鍵盤的信息
- - (void)viewDidLoad
- {
- NSLog(@"%@",NSStringFromSelector(_cmd));
- [super viewDidLoad];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
- }
檢測鍵盤消息一個六種,根據字面意思差不多都能說明函數作用
UIKeyboardWillShowNotification 通知將要發佈時候顯示鍵盤
UIKeyboardDidShowNotification 通知發佈後立即顯示鍵盤
UIKeyboardWillHideNotification 通知發佈前撤銷鍵盤
UIKeyboardDidHideNotification 通知發佈後撤銷鍵盤
UIKeyboardWillChangeFrameNotification 通知發佈前迅速變化的框架的鍵盤。
UIKeyboardDidChangeFrameNotification 通知發佈後立即改變在鍵盤的框架。
NSLog(@"%@",NSStringFromSelector(_cmd));是我特意加上去的,它能在控制檯顯示打印出當前程序所調用的函數,我在下面每個函數都加了這一句,當我進行不同操作的時候,打印出被調用函數名,在調試程序時候比較適用吧;
註冊消息通知後,實現通知所響應的方法
- - (void)handleKeyboardDidShow:(NSNotification *)notification
- {
- NSLog(@"%@",NSStringFromSelector(_cmd));
- NSDictionary *info = [notification userInfo];
- CGRect keyboardFrame;
- [[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
- CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;
- CGFloat distanceToMove = kbSize.height;
- NSLog(@"---->動態鍵盤高度:%f",distanceToMove);
- if (exitButton == nil) {
- exitButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- CGRect exitBtFrame = CGRectMake(self.view.frame.size.width-40, self.view.frame.size.height - distanceToMove, 40.0f, 30.0f);
- exitButton.frame = exitBtFrame;
- [exitButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateNormal];
- [self.view addSubview:exitButton];
- }
- exitButton.hidden=NO;
- [self adjustPanelsWithKeyBordHeight:distanceToMove];
- [exitButton addTarget:self action:@selector(CancelBackKeyboard:) forControlEvents:UIControlEventTouchDown];
- }
在這個函數方法中值得探討的是關於鍵盤所包含信息,因爲每一次鍵盤彈出的時候也是動畫形式彈出,他的座標位置大小包含在userInfo的字典中,現在我用
NSLog(@"-->info:%@",info);打印出info對象,這些信息都可以在不同存儲類型,取值的時候注意取值方式,此處只是提一提,希望以後有時間在做探討,
- - (void)handleKeyboardWillHide:(NSNotification *)notification
- {
- NSLog(@"%@",NSStringFromSelector(_cmd));
- if (exitButton.hidden==NO) {
- exitButton.hidden = YES;
- }
- // if (exitButton.superview)
- // {
- // [exitButton removeFromSuperview];
- // [exitButton release];
- // }
- }
- -(void)adjustPanelsWithKeyBordHeight:(float) height
- {
- NSLog(@"%@",NSStringFromSelector(_cmd));
- if (exitButton) {
- CGRect exitBtFrame = CGRectMake(self.view.frame.size.width - 40, self.view.frame.size.height - height-30, 40.0f, 30.0f);
- exitButton.frame = exitBtFrame;
- [self.view addSubview:exitButton];
- }
- // UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
- // if (exitButton.superview == nil)
- // {
- // [tempWindow addSubview:exitButton];
- // // 注意這裏直接加到window上
- // }
- }
- -(void)CancelBackKeyboard:(id)sender
- {
- NSLog(@"%@",NSStringFromSelector(_cmd));
- [textField resignFirstResponder];
- }
- - (void)viewDidUnload
- {
- [self setTextField:nil];
- exitButton=nil;
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- }
- - (void)dealloc {
- [textField release];
- [exitButton release];
- [[NSNotificationCenter defaultCenter] removeObserver:self];//移除所註冊的通知
- [super dealloc];
- }