Responder+Keyboard.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // Responder+Keyboard.m
  3. // UIKitSupport
  4. //
  5. // Created by RICHWARE SYSTEMS.
  6. // Copyright © 2016년 RICHWARE SYSTEMS. All rights reserved.
  7. //
  8. #import <objc/runtime.h>
  9. #import "Responder+Keyboard.h"
  10. static BOOL hasAlreadyCachedKeyboard;
  11. @interface UIResponder (Keyboard_Private)
  12. +(void) __cacheKeyboard;
  13. //@property (strong, nonatomic) id keyboardFrameChangedDelegate ;
  14. @end
  15. @implementation UIResponder(Keyboard)
  16. +(void) cacheKeyboard {
  17. [[self class] cacheKeyboard:NO];
  18. }
  19. +(void) cacheKeyboard:(BOOL)onNextRunloop {
  20. if (! hasAlreadyCachedKeyboard)
  21. {
  22. hasAlreadyCachedKeyboard = YES;
  23. if (onNextRunloop)
  24. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.0), dispatch_get_main_queue(), ^(void){ [[self class] __cacheKeyboard]; });
  25. else
  26. [[self class] __cacheKeyboard];
  27. }
  28. }
  29. +(void) __cacheKeyboard {
  30. UITextField *field = [UITextField new];
  31. [[[[UIApplication sharedApplication] windows] lastObject] addSubview:field];
  32. [field becomeFirstResponder];
  33. [field resignFirstResponder];
  34. [field removeFromSuperview];
  35. }
  36. //Delegate
  37. //- (id)keyboardFrameChangedDelegate {
  38. //
  39. // return objc_getAssociatedObject(self, @selector(keyboardFrameChangedDelegate));
  40. //
  41. //}
  42. //
  43. //- (void)setKeyboardFrameChangedDelegate:(id)keyboardFrameChangedDelegate {
  44. //
  45. // objc_setAssociatedObject(self, @selector(keyboardFrameChangedDelegate), keyboardFrameChangedDelegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  46. //
  47. //}
  48. //Keyboard
  49. - (void)addObserverKeyboardDisplay:(KeyboardDisplayBlock)keyboardDisplayBlock {
  50. [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification
  51. object:nil
  52. queue:[NSOperationQueue mainQueue]
  53. usingBlock:^(NSNotification * _Nonnull noti) {
  54. [self __keyboardWillChange:noti
  55. display:YES
  56. keyboardDisplayBlock:keyboardDisplayBlock] ;
  57. }] ;
  58. [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillHideNotification
  59. object:nil
  60. queue:[NSOperationQueue mainQueue]
  61. usingBlock:^(NSNotification * _Nonnull noti) {
  62. [self __keyboardWillChange:noti
  63. display:NO
  64. keyboardDisplayBlock:keyboardDisplayBlock] ;
  65. }] ;
  66. }
  67. - (void)__keyboardWillChange:(NSNotification*)noti display:(BOOL)display keyboardDisplayBlock:(KeyboardDisplayBlock)keyboardDisplayBlock {
  68. NSDictionary* info = [noti userInfo];
  69. NSTimeInterval duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey]floatValue] ;
  70. CGRect keyboardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
  71. if( keyboardDisplayBlock != nil ) {
  72. keyboardDisplayBlock(keyboardFrame, duration, display) ;
  73. }
  74. }
  75. //Keyboard by delegate
  76. - (void)addObserverKeyboardFrameChanged:(id)delegate {
  77. //self.keyboardFrameChangedDelegate = delegate ;
  78. [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification
  79. object:nil
  80. queue:[NSOperationQueue mainQueue]
  81. usingBlock:^(NSNotification * _Nonnull noti) {
  82. [self __keyboardWillChangeByDelegate:delegate
  83. noti:noti
  84. display:YES] ;
  85. }] ;
  86. [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillHideNotification
  87. object:nil
  88. queue:[NSOperationQueue mainQueue]
  89. usingBlock:^(NSNotification * _Nonnull noti) {
  90. [self __keyboardWillChangeByDelegate:delegate
  91. noti:noti
  92. display:NO] ;
  93. }] ;
  94. }
  95. - (void)__keyboardWillChangeByDelegate:(id)delegate noti:(NSNotification*)noti display:(BOOL)display {
  96. NSDictionary* info = [noti userInfo];
  97. NSTimeInterval duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey]floatValue] ;
  98. CGRect keyboardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
  99. if( delegate && [delegate respondsToSelector:@selector(keyboardFrameChanged:duration:display:)] ) {
  100. [delegate keyboardFrameChanged:keyboardFrame
  101. duration:duration
  102. display:display] ;
  103. }
  104. }
  105. - (void)removeObserverKeyboard {
  106. [[NSNotificationCenter defaultCenter] removeObserver:self
  107. name:UIKeyboardWillShowNotification
  108. object:nil] ;
  109. [[NSNotificationCenter defaultCenter] removeObserver:self
  110. name:UIKeyboardWillHideNotification
  111. object:nil] ;
  112. }
  113. @end