首先,自然是先监听键盘变化事件UIKeyboardWillChangeFrameNotification,代码如下:
//使用NSNotificationCenter
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
在keyboardWillChangeFrame方法中,通过判断键盘变化前后y轴和高度的变化,来判断键盘的状态,代码如下:
- (void)keyboardWillChangeFrame:(NSNotification *)notification{
// NSLog(@"userInfo:%@", notification.userInfo);
CGRect keyboredBeginFrame = [notification.userInfo[@"UIKeyboardFrameBeginUserInfoKey"] CGRectValue];
CGRect keyboredEndFrame = [notification.userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
CGFloat duration = [notification.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];
// N