Last active
December 26, 2015 02:39
-
-
Save atkit/7080340 to your computer and use it in GitHub Desktop.
Stick to Keyboard Animation Curve
This file contains 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
// We may use it in viewWillAppear/viewWillDisappear | |
- (void) addKeyboardObserver | |
{ | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; | |
} | |
- (void) removeKeyboardObserver | |
{ | |
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; | |
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; | |
} | |
// Notifcation Callbacks | |
- (void)keyboardWillShow:(NSNotification *)notification | |
{ | |
[self saveContainerOriginalFrame]; // save current state, application detail | |
[self showKeyboardCloseBarButton]; // put keyboard close button somehwere | |
[self keyboardAnimateBlock:^(CGFloat keyboardHeight){ | |
[self setContainerViewFrameWhenKeyboardHasHeight:keyboardHeight]; // application but we use keyboard height | |
} usingNotification:notification]; | |
} | |
- (void)keyboardWillHide:(NSNotification *)notification | |
{ | |
[self hideKeyboardCloseBarButton]; // remove keyboard close button if any | |
[self keyboardAnimateBlock:^(CGFloat keyboardHeight){ | |
[self setContainerViewToOriginalFrame]; // application detail | |
} usingNotification:notification]; | |
} | |
// Actual animate Keyboard Block | |
- (void)keyboardAnimateBlock:(void(^)(CGFloat))block usingNotification:(NSNotification *)notification | |
{ | |
NSDictionary *userInfo = [notification userInfo]; | |
NSTimeInterval animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; | |
UIViewAnimationCurve animationCurve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]; | |
CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; | |
CGFloat keyboardHeight = keyboardFrame.size.height; | |
[UIView animateWithDuration:animationDuration delay:0.0 options:(animationCurve | UIViewAnimationOptionBeginFromCurrentState) animations:^{ | |
block(keyboardHeight); | |
} completion:nil]; | |
} | |
// let me know if anything was missed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment