ios键盘监听
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ios键盘监听相关的知识,希望对你有一定的参考价值。
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 }
以上是关于ios键盘监听的主要内容,如果未能解决你的问题,请参考以下文章