| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- //
- // Responder+Keyboard.m
- // UIKitSupport
- //
- // Created by RICHWARE SYSTEMS.
- // Copyright © 2016년 RICHWARE SYSTEMS. All rights reserved.
- //
- #import <objc/runtime.h>
- #import "Responder+Keyboard.h"
- static BOOL hasAlreadyCachedKeyboard;
- @interface UIResponder (Keyboard_Private)
- +(void) __cacheKeyboard;
- //@property (strong, nonatomic) id keyboardFrameChangedDelegate ;
- @end
- @implementation UIResponder(Keyboard)
- +(void) cacheKeyboard {
- [[self class] cacheKeyboard:NO];
- }
- +(void) cacheKeyboard:(BOOL)onNextRunloop {
- if (! hasAlreadyCachedKeyboard)
- {
- hasAlreadyCachedKeyboard = YES;
- if (onNextRunloop)
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.0), dispatch_get_main_queue(), ^(void){ [[self class] __cacheKeyboard]; });
- else
- [[self class] __cacheKeyboard];
- }
- }
- +(void) __cacheKeyboard {
- UITextField *field = [UITextField new];
- [[[[UIApplication sharedApplication] windows] lastObject] addSubview:field];
- [field becomeFirstResponder];
- [field resignFirstResponder];
- [field removeFromSuperview];
- }
- //Delegate
- //- (id)keyboardFrameChangedDelegate {
- //
- // return objc_getAssociatedObject(self, @selector(keyboardFrameChangedDelegate));
- //
- //}
- //
- //- (void)setKeyboardFrameChangedDelegate:(id)keyboardFrameChangedDelegate {
- //
- // objc_setAssociatedObject(self, @selector(keyboardFrameChangedDelegate), keyboardFrameChangedDelegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- //
- //}
- //Keyboard
- - (void)addObserverKeyboardDisplay:(KeyboardDisplayBlock)keyboardDisplayBlock {
-
- [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification
- object:nil
- queue:[NSOperationQueue mainQueue]
- usingBlock:^(NSNotification * _Nonnull noti) {
-
- [self __keyboardWillChange:noti
- display:YES
- keyboardDisplayBlock:keyboardDisplayBlock] ;
-
- }] ;
-
- [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillHideNotification
- object:nil
- queue:[NSOperationQueue mainQueue]
- usingBlock:^(NSNotification * _Nonnull noti) {
-
- [self __keyboardWillChange:noti
- display:NO
- keyboardDisplayBlock:keyboardDisplayBlock] ;
-
- }] ;
-
- }
- - (void)__keyboardWillChange:(NSNotification*)noti display:(BOOL)display keyboardDisplayBlock:(KeyboardDisplayBlock)keyboardDisplayBlock {
-
- NSDictionary* info = [noti userInfo];
-
- NSTimeInterval duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey]floatValue] ;
- CGRect keyboardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
-
- if( keyboardDisplayBlock != nil ) {
- keyboardDisplayBlock(keyboardFrame, duration, display) ;
- }
-
- }
- //Keyboard by delegate
- - (void)addObserverKeyboardFrameChanged:(id)delegate {
-
- //self.keyboardFrameChangedDelegate = delegate ;
-
- [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification
- object:nil
- queue:[NSOperationQueue mainQueue]
- usingBlock:^(NSNotification * _Nonnull noti) {
-
- [self __keyboardWillChangeByDelegate:delegate
- noti:noti
- display:YES] ;
-
- }] ;
-
- [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillHideNotification
- object:nil
- queue:[NSOperationQueue mainQueue]
- usingBlock:^(NSNotification * _Nonnull noti) {
-
- [self __keyboardWillChangeByDelegate:delegate
- noti:noti
- display:NO] ;
-
- }] ;
-
- }
- - (void)__keyboardWillChangeByDelegate:(id)delegate noti:(NSNotification*)noti display:(BOOL)display {
-
- NSDictionary* info = [noti userInfo];
-
- NSTimeInterval duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey]floatValue] ;
- CGRect keyboardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
-
- if( delegate && [delegate respondsToSelector:@selector(keyboardFrameChanged:duration:display:)] ) {
-
- [delegate keyboardFrameChanged:keyboardFrame
- duration:duration
- display:display] ;
-
- }
-
- }
- - (void)removeObserverKeyboard {
-
- [[NSNotificationCenter defaultCenter] removeObserver:self
- name:UIKeyboardWillShowNotification
- object:nil] ;
-
- [[NSNotificationCenter defaultCenter] removeObserver:self
- name:UIKeyboardWillHideNotification
- object:nil] ;
-
- }
- @end
|