1 // 监听键盘 2 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowAction:) name:UIKeyboardWillShowNotification object:nil]; 3 4 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideAction:) name:UIKeyboardWillHideNotification object:nil];
1 #pragma mark - 键盘处理 2 3 /** 4 * 键盘即将隐藏 5 */ 6 - (void)keyboardWillHideAction:(NSNotification *)note 7 { 8 9 // 1.键盘弹出需要的时间 10 CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 11 12 // 2.动画 13 [UIView animateWithDuration:duration animations:^{ 14 self.view.transform = CGAffineTransformIdentity; 15 }]; 16 } 17 18 /** 19 * 键盘即将弹出 20 */ 21 - (void)keyboardWillShowAction:(NSNotification *)note 22 { 23 24 // 1.键盘弹出需要的时间 25 CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 26 27 // 2.动画 28 [UIView animateWithDuration:duration animations:^{ 29 // 取出键盘高度 30 CGRect keyboardF = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; 31 CGFloat keyboardH = keyboardF.size.height - 40; 32 if (ThreeInch) { 33 self.view.transform = CGAffineTransformMakeTranslation(0, - keyboardH); 34 } else { 35 self.view.transform = CGAffineTransformMakeTranslation(0, - 50); 36 } 37 }]; 38 }
时间: 2024-10-12 19:50:16