Skip to content

Instantly share code, notes, and snippets.

@danielctull
Last active August 29, 2015 14:07
Show Gist options
  • Save danielctull/abd046a754199ef15b52 to your computer and use it in GitHub Desktop.
Save danielctull/abd046a754199ef15b52 to your computer and use it in GitHub Desktop.
@import UIKit;
@interface ASRKeyboardNotificationValues : NSObject
- (instancetype)initWithNotification:(NSNotification *)notification;
@property (nonatomic, readonly) NSNotification *notification;
@property (nonatomic, readonly) NSTimeInterval animationDuration;
@property (nonatomic, readonly) UIViewAnimationCurve animationCurve;
@property (nonatomic, readonly) UIViewAnimationOptions animationOptions;
@property (nonatomic, readonly) CGRect frameBegin;
@property (nonatomic, readonly) CGRect frameEnd;
@end
#import "ASRKeyboardNotificationValues.h"
@implementation ASRKeyboardNotificationValues
- (instancetype)initWithNotification:(NSNotification *)notification {
self = [self init];
if (!self) return nil;
_notification = notification;
return self;
}
- (NSTimeInterval)animationDuration {
return [self.notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
}
- (UIViewAnimationCurve)animationCurve {
return [self.notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue];
}
- (UIViewAnimationOptions)animationOptions {
return [self animationCurve] << 16;
}
- (CGRect)frameBegin {
return [self frameForKey:UIKeyboardFrameBeginUserInfoKey];
}
- (CGRect)frameEnd {
return [self frameForKey:UIKeyboardFrameEndUserInfoKey];
}
- (CGRect)frameForKey:(NSString *)key {
NSValue *value = self.notification.userInfo[key];
if ([value isKindOfClass:[NSValue class]]) {
return value.CGRectValue;
}
return CGRectNull;
}
@end
class SomeViewController : UIViewController {
@IBOutlet weak var bottomConstraint: NSLayoutConstraint!
func keyboardWillChangeFrameNotification(notification: NSNotification) {
let values = ASRKeyboardNotificationValues(notification: notification)
self.bottomConstraint.constant = values.frameEnd.size.height
UIView.animateWithDuration(values.animationDuration, delay:0, options:values.animationOptions, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment