NIMKitKeyboardInfo.m 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //
  2. // NIMKitKeyboardInfo.m
  3. // NIMKit
  4. //
  5. // Created by chris on 2017/11/3.
  6. // Copyright © 2017年 NetEase. All rights reserved.
  7. //
  8. #import "NIMKitKeyboardInfo.h"
  9. NSNotificationName const NIMKitKeyboardWillChangeFrameNotification = @"NIMKitKeyboardWillChangeFrameNotification";
  10. NSNotificationName const NIMKitKeyboardWillHideNotification = @"NIMKitKeyboardWillHideNotification";
  11. @implementation NIMKitKeyboardInfo
  12. @synthesize keyboardHeight = _keyboardHeight;
  13. + (instancetype)instance
  14. {
  15. static NIMKitKeyboardInfo *instance;
  16. static dispatch_once_t onceToken;
  17. dispatch_once(&onceToken, ^{
  18. instance = [[NIMKitKeyboardInfo alloc] init];
  19. });
  20. return instance;
  21. }
  22. - (instancetype)init
  23. {
  24. self = [super init];
  25. if (self)
  26. {
  27. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
  28. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youpaifkeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  29. }
  30. return self;
  31. }
  32. - (void)keyboardWillChangeFrame:(NSNotification *)notification
  33. {
  34. NSDictionary *userInfo = notification.userInfo;
  35. CGRect endFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  36. _isVisiable = endFrame.origin.y != [UIApplication sharedApplication].keyWindow.frame.size.height;
  37. _keyboardHeight = _isVisiable? endFrame.size.height: 0;
  38. [[NSNotificationCenter defaultCenter] postNotificationName:NIMKitKeyboardWillChangeFrameNotification object:nil userInfo:notification.userInfo];
  39. }
  40. - (void)youpaifkeyboardWillHide:(NSNotification *)notification
  41. {
  42. _isVisiable = NO;
  43. _keyboardHeight = 0;
  44. [[NSNotificationCenter defaultCenter] postNotificationName:NIMKitKeyboardWillHideNotification object:nil userInfo:notification.userInfo];
  45. }
  46. @end