今天在写键盘弹出和退出时候,利用通知监听事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
偶然发现在使用搜狗第三方键盘时候,有些卡顿。
打断点发现:
键盘弹出的时候,UIKeyboardWillShowNotification,这个通知监听的方法居然进了3次,而且监听到的键盘高度居然不一致。
最终的解决方法:
#pragma mark - 键盘通知的方法
-(void)keyboardWillShow:(NSNotification *)notification{
NSDictionary *keyBordInfo = [notification userInfo];
DDLog(@"keyBordInfo = %@",keyBordInfo);
NSValue *value = [keyBordInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyBoardRect = [value CGRectValue];
float height = keyBoardRect.size.height;
CGRect beginRect = [[keyBordInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect endRect = [[keyBordInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
DDLog(@"keyBordInfo.height = %f",height);
// 第三方键盘回调三次问题,监听仅执行最后一次
if(beginRect.size.height > 0 && (beginRect.origin.y - endRect.origin.y > 0)){
//do someing
}
}