RSKKeyboardAnimationObserver
  • March 11, 2024

Easy way to handle iOS keyboard showing/dismissing.

Introduction


Working with iOS keyboard demands a lot of duplicated code. This category allows you to declare your animations with smooth keyboard animation timing while writing very little code.

Demo


Installation


RSKKeyboardAnimationObserver requires iOS 6.0 or later.

Using CocoaPods

  1. Add the pod RSKKeyboardAnimationObserver to your Podfile.

pod ‘RSKKeyboardAnimationObserver’

  1. Run pod install from Terminal, then open your app’s .xcworkspace file to launch Xcode.

  2. Import the RSKKeyboardAnimationObserver.h header. Typically, this should be written as #import <RSKKeyboardAnimationObserver/RSKKeyboardAnimationObserver.h>

Using Carthage

  1. Add the ruslanskorb/RSKKeyboardAnimationObserver project to your Cartfile.

github “ruslanskorb/RSKKeyboardAnimationObserver”

  1. Run carthage update, then follow the additional steps required to add the iOS and/or Mac frameworks into your project.

  2. Import the RSKKeyboardAnimationObserver framework/module.

    • Using Modules: @import RSKKeyboardAnimationObserver
    • Without Modules: #import <RSKKeyboardAnimationObserver/RSKKeyboardAnimationObserver.h>

Example


Imagine that you need to implement chat-like input over keyboard. OK, import this category.

#import <RSKKeyboardAnimationObserver/RSKKeyboardAnimationObserver.h>

Then make autolayout constraint between your input bottom and superview botton in Interface Builder, connect it with your view controller implementation through IBOutlet.

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *chatInputBottomSpace;

Then subscribe to keyboard in the place you like (viewDidAppear is the best place really).

__weak typeof(self) weakSelf = self;
[self rsk_subscribeKeyboardWithWillShowOrHideAnimation:^(CGRect keyboardRectEnd, NSTimeInterval duration, BOOL isShowing) {
__strong typeof(self) strongSelf = weakSelf;
if (strongSelf) {
strongSelf.chatInputBottomSpace.constant = isShowing ? CGRectGetHeight(keyboardRectEnd) : 0;
[strongSelf.view layoutIfNeeded];
}
} onComplete:nil];

That’s all!

Don’t forget to unsubscribe from keyboard events (viewDidDisappear is my recommendation). Calling this category method will do all the “dirty” work for you.

[self rsk_unsubscribeKeyboard];

For more complex behaviour (like in demo section) you can use extended API call with before animation section.

__weak typeof(self) weakSelf = self;
[self rsk_subscribeKeyboardWithBeforeWillShowOrHideAnimation:^(CGRect keyboardRectEnd, NSTimeInterval duration, BOOL isShowing) {
__strong typeof(self) strongSelf = weakSelf;
if (strongSelf) {
strongSelf.isKeaboardAnimation = YES;

[UIView transitionWithView:strongSelf.imageView duration:duration options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
if (isShowing) {
strongSelf.imageView.image = [strongSelf.imageView.image applyLightEffect];
} else {
[strongSelf.imageView hnk_setImageFromURL:strongSelf.model.cardImageUrl];
}
} completion:nil];
}
} willShowOrHideAnimation:^(CGRect keyboardRectEnd, NSTimeInterval duration, BOOL isShowing) {
__strong typeof(self) strongSelf = weakSelf;
if (strongSelf) {
strongSelf.headerHeight.constant = isShowing ? kHeaderMinHeight : kHeaderMaxHeight;
strongSelf.panelSpace.constant = isShowing ? CGRectGetHeight(keyboardRectEnd) : 0;

for (UIView *v in strongSelf.headerAlphaViews) {
v.alpha = isShowing ? 0.0f : 1.0f;
}

[strongSelf.view layoutIfNeeded];
}
} onComplete:^(BOOL finished, BOOL isShown) {
__strong typeof(self) strongSelf = weakSelf;
if (strongSelf) {
strongSelf.isKeaboardAnimation = NO;
}
}];

GitHub


View Github

#animation #hide #keyboard #show
YOU MIGHT ALSO LIKE...
exyte

     

camerakit-ios

CameraKit helps you add reliable camera to your app quickly. Our open source camera platform provides consistent capture results, service ...

HybridCamera

[video width="192" height="416" mp4="https://swiftgit.com/wp-content/uploads/2024/12/68747470733a2f2f7261776769742e636f6d2f7374796c656b69742f696d672f6d61737465722f7669645f6564697465645f325f326d622e676966.mp4"][/video]

TakeASelfie

An iOS framework that uses the front camera, detects your face and takes a selfie. This api opens the front ...

iOS-Depth-Sampler

Code examples of Depth APIs in iOS