Last active
August 29, 2015 14:13
-
-
Save slabko/d4cc07aeef5dc238273f to your computer and use it in GitHub Desktop.
NSNotification + KeyboardNotifications
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
@interface NSNotification (KeyboardNotifications) | |
- (NSTimeInterval)keyboardAnimationDuration; | |
- (UIViewAnimationCurve)keyboardAnimationCurve; | |
- (CGRect)keyboardFrame; | |
- (void)animateSynchronouslyWithKeyboard:(void(^)(void))animation completion:(void(^)(void))completion; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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