iOS开发第三方键盘处理实例代码

2020-01-20 23:18:59丽君

最近项目中遇到了键盘处理通知被调用多次的情况,废了好半天时间才找到解决办法,今天就给小伙伴儿们唠唠第三方键盘处理的那些坑!

详情请看:『https://www.easck.com/pre>

3、具体解决办法

但是我项目中得复杂情况就不行了,键盘弹起异常,收回也异常,尤其是用了聊天室这种view,处理的更麻烦,不要急,看看博爱这些年踩过的坑吧:

先看看键盘处理事件吧:


- 1、首先注册通知:
- (void)registNotification
{
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardWillHideNotification object:nil];
}

 

- 2、再把后路想好:移除通知
- (void)removeNotification
{
 [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- 3、通知事件处理:
/*! 键盘显示要做什么 */
- (void)keyboardWasShown:(NSNotification *)notification
{
 NSDictionary *info         = [notification userInfo];

 double duration          = [info[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

 CGFloat curkeyBoardHeight       = [[info objectForKey:@"UIKeyboardBoundsUserInfoKey"] CGRectValue].size.height;
 CGRect begin          = [[info objectForKey:@"UIKeyboardFrameBeginUserInfoKey"] CGRectValue];
 CGRect end           = [[info objectForKey:@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];

 CGFloat keyBoardHeight;

 /*! 第三方键盘回调三次问题,监听仅执行最后一次 */
 if(begin.size.height > 0 && (begin.origin.y - end.origin.y > 0))
 {
  keyBoardHeight         = curkeyBoardHeight;
  [UIView animateWithDuration:duration animations:^{

   CGRect viewFrame       = [self getCurrentViewController].view.frame;
   viewFrame.origin.y -= keyBoardHeight;
   [self getCurrentViewController].view.frame = viewFrame;
  }];
 }
}

- (void)keyboardWasHidden:(NSNotification *)notification
{
 NSDictionary *info = [notification userInfo];
 double duration = [info[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

 [UIView animateWithDuration:duration animations:^{

  CGRect viewFrame = [self getCurrentViewController].view.frame;
  viewFrame.origin.y = 0;
  [self getCurrentViewController].view.frame = viewFrame;
 }];
}

/*!
* 获取当前View的VC
*
* @return 获取当前View的VC
*/
- (UIViewController *)getCurrentViewController
{
 for (UIView *view = self; view; view = view.superview)
 {
 UIResponder *nextResponder = [view nextResponder];
 if ([nextResponder isKindOfClass:[UIViewController class]])
 {
 return (UIViewController *)nextResponder;
 }
 }
 return nil;
}

具体情况是这样的,在测试过程中,其他界面的评论框都没问题,就直播这个VC有问题,就一步步往下找,后来发现:iOS的第三方键盘会在【- (void)keyboardWasShown:(NSNotification *)notification】这个方法中来回调用多次,不止三次好像,然后就想到一个办法,