ios開發之--鍵盤的監聽
阿新 • • 發佈:2017-07-13
efault key nil self. transform blog mar form owa
監聽鍵盤的彈出,讓整個頁面向上移動,比較簡單,僅做記錄使用:
代碼如下:
// 監聽鍵盤 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowAction:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideAction:) name:UIKeyboardWillHideNotificationobject:nil];
鍵盤處理:
#pragma mark - 鍵盤處理 /** * 鍵盤即將隱藏 */ - (void)keyboardWillHideAction:(NSNotification *)note { // 1.鍵盤彈出需要的時間 CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; // 2.動畫 [UIView animateWithDuration:duration animations:^{ self.view.transform= CGAffineTransformIdentity; }]; } /** * 鍵盤即將彈出 */ - (void)keyboardWillShowAction:(NSNotification *)note { // 1.鍵盤彈出需要的時間 CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; // 2.動畫 [UIView animateWithDuration:duration animations:^{// 取出鍵盤高度 CGRect keyboardF = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGFloat keyboardH = keyboardF.size.height - 40; if (_ThreeInch) { self.view.transform = CGAffineTransformMakeTranslation(0, - keyboardH); } else { self.view.transform = CGAffineTransformMakeTranslation(0, - 50); } }]; }
ios開發之--鍵盤的監聽