Code Monkey home page Code Monkey logo

vfkeyboardobserver's Introduction

VFKeyboardObserver

[CI Status](https://travis-ci.org/Valery Fomenko/VFKeyboardObserver) Version License Platform

A simple way to observe keyboard notifications and keyboard properties.

Usage

  1. Start / stop observing keyboard events. ==
// Create new observer, or use shared instance
[[VFKeyboardObserver sharedKeyboardObserver] start];
[[VFKeyboardObserver sharedKeyboardObserver] stop];
  1. Add / remove delegates ==

Adopt to VFKeyboardObserverDelegate protocol and add an object as a delegate.

VFKeyboardObserver can have any number of delegates. Delegates are not retained.

@protocol VFKeyboardObserverDelegate <NSObject>

@optional

- (void)keyboardObserver:(VFKeyboardObserver *)keyboardObserver keyboardWillShowWithProperties:(VFKeyboardProperties)keyboardProperties interfaceOrientationWillChange:(BOOL)interfaceOrientationWillChange;
- (void)keyboardObserver:(VFKeyboardObserver *)keyboardObserver keyboardDidShowWithProperties:(VFKeyboardProperties)keyboardProperties;

- (void)keyboardObserver:(VFKeyboardObserver *)keyboardObserver keyboardWillHideWithProperties:(VFKeyboardProperties)keyboardProperties;
- (void)keyboardObserver:(VFKeyboardObserver *)keyboardObserver keyboardDidHideWithProperties:(VFKeyboardProperties)keyboardProperties;

@end

[[VFKeyboardObserver sharedKeyboardObserver] addDelegate:aDelegate];
[[VFKeyboardObserver sharedKeyboardObserver] removeDelegate:aDelegate];
  1. Update UI when keyboard appears / disappears ==

It is common to update UI when keyboard will appear / will disappear.

- (void)keyboardObserver:(VFKeyboardObserver *)keyboardObserver keyboardWillShowWithProperties:(VFKeyboardProperties)keyboardProperties interfaceOrientationWillChange:(BOOL)interfaceOrientationWillChange {
    [keyboardObserver animateWithKeyboardProperties:^{
        // ...
    }];
}

- (void)keyboardObserver:(VFKeyboardObserver *)keyboardObserver keyboardWillHideWithProperties:(VFKeyboardProperties)keyboardProperties {
    [keyboardObserver animateWithKeyboardProperties:^{
        // ...
    }];
}

VFKeyboardObserver have convenient method animateWithKeyboardProperties: that performs animations synched with keyboard animation.

  1. Rotation ==

If we animate our UI to adjust it to keyboard, for example, attach UITextField to the top of the keyboard, there is one problem when interface orientation changes. Our UITextField does not stay attached while the rotation of interface orientation is animated. See this for details: http://smnh.me/synchronizing-rotation-animation-between-the-keyboard-and-the-attached-view/

To solve this problem, VFKeyboardObserver has interfaceOrientationWillChange method. Call it from UIViewController's viewWillTransitionToSize:withTransitionCoordinator:.

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    [[VFKeyboardObserver sharedKeyboardObserver] interfaceOrientationWillChange];
}

In this case, (only while interface orientation changes):

  • VFKeyboardObserverDelegate will receive only two messages: keyboardWillShow and keyboardDidShow (hide methods will be ignored).
  • interfaceOrientationWillChange argument of keyboardWillShow delegate method will be YES, so in case of custom UI adjusting animations in this method, you can do it without animation
  • animateWithKeyboardProperties: methods automatically perform animations and completion blocks without animation (if you use it, no changes needed).

Requirements

Installation

VFKeyboardObserver is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "VFKeyboardObserver"

Author

Valery Fomenko, [email protected]

License

VFKeyboardObserver is available under the MIT license. See the LICENSE file for more info.

vfkeyboardobserver's People

Contributors

vlfm avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

carabina

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.