美文网首页IOSiOS DeveloperiOS学习笔记
iOS中使用第三方键盘,通知监听事件执行多次的解决方法

iOS中使用第三方键盘,通知监听事件执行多次的解决方法

作者: 我是七月 | 来源:发表于2017-06-07 22:43 被阅读1160次
奋斗的七月
今天在写键盘弹出和退出时候,利用通知监听事件
[[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

    }
}

相关文章

网友评论

    本文标题:iOS中使用第三方键盘,通知监听事件执行多次的解决方法

    本文链接:https://www.haomeiwen.com/subject/upaafxtx.html