Coder Social home page Coder Social logo

kwkeyboardlistener's Introduction

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'

kwkeyboardlistener's People

Contributors

altimor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.