BaseVC+KeyboardObserve.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // BaseVC+KeyboardObserve.m
  3. // 乐销
  4. //
  5. // Created by 隋林栋 on 2017/1/20.
  6. // Copyright © 2017年 ping. All rights reserved.
  7. //
  8. #import "BaseVC+KeyboardObserve.h"
  9. static const char firstResponserViewKey = '\0';
  10. static const char isKeyboardObserveKey = '\0';
  11. @implementation BaseVC (KeyboardObserve)
  12. #pragma mark 运行时
  13. - (void)setIsKeyboardObserve:(BOOL)isKeyboardObserve{
  14. objc_setAssociatedObject(self, &isKeyboardObserveKey, [NSNumber numberWithBool:isKeyboardObserve], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  15. }
  16. - (BOOL)isKeyboardObserve{
  17. NSNumber * num = objc_getAssociatedObject(self, &isKeyboardObserveKey);
  18. return (num && [num isKindOfClass:NSNumber.self])?[num boolValue]:false;
  19. }
  20. - (void)setFirstResponserView:(UIView *)firstResponserView{
  21. objc_setAssociatedObject(self, &firstResponserViewKey, firstResponserView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  22. }
  23. - (UIView *)firstResponserView{
  24. UIView * view = objc_getAssociatedObject(self, &firstResponserViewKey);
  25. if (view == nil) {
  26. view = [self.view fetchFirstResponder];
  27. [self setFirstResponserView:view];
  28. }
  29. return view;
  30. }
  31. #pragma mark //添加键盘监听
  32. - (void)addKeyboardObserve{
  33. NSNotificationCenter * nCenter = [NSNotificationCenter defaultCenter];
  34. [nCenter addObserver:self selector:@selector(myTextFieldDidEndEditing:) name:UITextFieldTextDidEndEditingNotification object:nil];
  35. [nCenter addObserver:self selector:@selector(myTextFieldDidEndEditing:) name:UITextViewTextDidEndEditingNotification object:nil];
  36. [nCenter addObserver:self selector:@selector(keyboardDidChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
  37. }
  38. - (void)removeKeyboardObserve{
  39. NSNotificationCenter * nCenter = [NSNotificationCenter defaultCenter];
  40. [nCenter removeObserver:self name:UITextFieldTextDidEndEditingNotification object:nil];
  41. [nCenter removeObserver:self name:UITextViewTextDidEndEditingNotification object:nil];
  42. [nCenter removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
  43. }
  44. - (void)myTextFieldDidEndEditing:(NSNotification *)noti{
  45. self.firstResponserView = nil;
  46. }
  47. //移动位置
  48. - (void)keyboardDidChangeFrame:(NSNotification *)noti
  49. {
  50. // 键盘的frame
  51. CGRect frame = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  52. // 键盘的实时Y
  53. CGFloat keyHeight = SCREEN_HEIGHT - frame.origin.y;
  54. // 屏幕的高度
  55. CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
  56. // 动画时间
  57. CGFloat keyDuration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey]floatValue];
  58. CGFloat yCorrect = 0;
  59. if (self.firstResponserView != nil ) {
  60. CGRect frame = [self.firstResponserView convertRect:self.firstResponserView.bounds toView:self.view];
  61. yCorrect = - ( SCREEN_HEIGHT - ( frame.origin.y + frame.size.height) - keyHeight - 120 );
  62. }
  63. if (yCorrect < 0) {
  64. yCorrect = 0;
  65. }
  66. if (yCorrect > keyHeight ) {//if on the bottom,remove keyHeight Most
  67. yCorrect = keyHeight;
  68. }
  69. // 执行动画
  70. [UIView animateWithDuration:keyDuration animations:^{
  71. CGFloat transformY = keyHeight == 0 ? 0 : - yCorrect;
  72. self.view.transform = CGAffineTransformMakeTranslation(0, transformY);
  73. }];
  74. }
  75. @end