Skip to content

Instantly share code, notes, and snippets.

@slabko
Last active August 29, 2015 14:13
Show Gist options
  • Save slabko/d4cc07aeef5dc238273f to your computer and use it in GitHub Desktop.
Save slabko/d4cc07aeef5dc238273f to your computer and use it in GitHub Desktop.
NSNotification + KeyboardNotifications
#import <UIKit/UIKit.h>
@interface NSNotification (KeyboardNotifications)
- (NSTimeInterval)keyboardAnimationDuration;
- (UIViewAnimationCurve)keyboardAnimationCurve;
- (CGRect)keyboardFrame;
- (void)animateSynchronouslyWithKeyboard:(void(^)(void))animation completion:(void(^)(void))completion;
@end
#import "NSNotification+KeyboardNotifications.h"
@implementation NSNotification (KeyboardNotifications)
- (NSTimeInterval)keyboardAnimationDuration
{
NSAssert([self.name isEqualToString:UIKeyboardWillShowNotification] ||
[self.name isEqualToString:UIKeyboardWillHideNotification], nil);
return [self.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
}
- (UIViewAnimationCurve)keyboardAnimationCurve
{
NSAssert([self.name isEqualToString:UIKeyboardWillShowNotification] ||
[self.name isEqualToString:UIKeyboardWillHideNotification], nil);
UIViewAnimationCurve curve;
[[self.userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&curve];
return curve;
}
- (CGRect)keyboardFrame
{
NSAssert([self.name isEqualToString:UIKeyboardWillShowNotification] ||
[self.name isEqualToString:UIKeyboardWillHideNotification], nil);
return [self.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
}
- (void)animateSynchronouslyWithKeyboard:(void(^)(void))animation completion:(void(^)(void))completion
{
NSParameterAssert(animation);
[UIView beginAnimations:nil context:(__bridge_retained void *)[completion copy]];
[UIView setAnimationDuration:[self keyboardAnimationDuration]];
[UIView setAnimationCurve:[self keyboardAnimationCurve]];
if (completion) {
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(keyboardAnimationDidStop:finished:context:)];
}
animation();
[UIView commitAnimations];
}
- (void)keyboardAnimationDidStop:(NSString *)animationID
finished:(NSNumber *)finished
context:(void *)context
{
if (context) {
void(^completion)(void) = (__bridge_transfer void(^)(void))context;
completion();
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment