Skip to content

kwarter/KWKeyboardListener

Repository files navigation

KWKeyboardListener

Easy way to make a class react to keyboard events.

##Register a new listener

Before:

- (void)registerMyClass {
   NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
   [defaultCenter addObserver:self selector:@selector(didShow:) name:UIKeyboardDidShowNotification object:nil];
   [defaultCenter addObserver:self selector:@selector(didHide:) name:UIKeyboardDidHideNotification object:nil];
   [defaultCenter addObserver:self selector:@selector(willShow:) name:UIKeyboardWillShowNotification object:nil];
   [defaultCenter addObserver:self selector:@selector(willHide:) name:UIKeyboardWillHideNotification object:nil];
   [defaultCenter addObserver:self selector:@selector(didChangeFrame:) name:UIKeyboardDidChangeFrameNotification object:nil];
   [defaultCenter addObserver:self selector:@selector(willChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

// No autocompletion here, you have to type the methods' name…
- (void)didShow:(NSNotification *)notification {
...
}

- (void)willShow:(NSNotification *)notification {
...
}

- (void)didHide:(NSNotification *)notification {
...
}

- (void)willHide:(NSNotification *)notification {
...
}

- (void)didChangeFrame:(NSNotification *)notification {
...
}

- (void)willChangeFrame:(NSNotification *)notification {
...
}
   

After:

- (void)registerMyClass {
  [[KWKeyboardListener sharedInstance] addKeyboardEventsListener:self withHandler:^(CGRect keyboardFrame, BOOL opening, BOOL closing) {
      // This block will get called when the keyboard *will* show/hide/change frame
  }];
}

You can also make your class conform to the KWKeyboardEventsListener protocol, call [[KWKeyboardListener sharedInstance] addKeyboardEventsListener:self] and implement the protocol's methods you wish. The advantage of this method over NSNotificationCenter is that you do not have to register your class one time for each event you want to react to, and thanks to the protocol you'll have auto-completion, so it is easy to react to a new event simply by begin typing - (void)keyboard and see the list of supported events, which are:

- (void)keyboardWillShowWithInfos:(NSDictionary *)infos;
- (void)keyboardWillHideWithInfos:(NSDictionary *)infos;
- (void)keyboardDidShowWithInfos:(NSDictionary *)infos;
- (void)keyboardDidHideWithInfos:(NSDictionary *)infos;
- (void)keyboardWillChangeFrameWithInfos:(NSDictionary *)infos;
- (void)keyboardDidChangeFrameWithInfos:(NSDictionary *)infos;

The infos are simply the userInfos you would normally receive from the regular NSNotification. You can get the informations relative to the event with the regular notificaton keys, as defined in UIKit:

UIKIT_EXTERN NSString *const UIKeyboardFrameBeginUserInfoKey        NS_AVAILABLE_IOS(3_2); // NSValue of CGRect
UIKIT_EXTERN NSString *const UIKeyboardFrameEndUserInfoKey          NS_AVAILABLE_IOS(3_2); // NSValue of CGRect
UIKIT_EXTERN NSString *const UIKeyboardAnimationDurationUserInfoKey NS_AVAILABLE_IOS(3_0); // NSNumber of double
UIKIT_EXTERN NSString *const UIKeyboardAnimationCurveUserInfoKey    NS_AVAILABLE_IOS(3_0); // NSNumber of NSUInteger 

##Know the keyboard state Moreover, the KWKeyboardListener singleton, which is instantiated at the class load, so you don't have to do anything, allows you to get the current state of the keyboard from anywhere in the app, using its keyboardVisible boolean property.

##Pod pod 'KWKeyboardListener', '~>1.0'

About

Easy way to make a class react to keyboard events.

Resources

License

Stars

Watchers

Forks

Packages

No packages published