-
-
Save skeeet/a6130f380ddc9a07b312 to your computer and use it in GitHub Desktop.
UIKeyboardWillChangeFrameNotification method
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
- (void)viewWillAppear:(BOOL)animated | |
{ | |
[super viewWillAppear:animated]; | |
[[NSNotificationCenter defaultCenter] addObserver:self | |
selector:@selector(keyboardWillChange:) | |
name:UIKeyboardWillChangeFrameNotification | |
object:nil]; | |
} | |
- (void)viewWillDisappear:(BOOL)animated | |
{ | |
[super viewWillDisappear:animated]; | |
[[NSNotificationCenter defaultCenter] removeObserver:self | |
name:UIKeyboardWillChangeFrameNotification | |
object:nil]; | |
} | |
#pragma mark - keyboard | |
// abstract | |
- (void)doKeyboardAnimateTo:(CGFloat)toValue | |
{ | |
} | |
// abstract | |
- (void)completedKeyboardAnimateTo:(CGFloat)toValue | |
{ | |
} | |
- (void)keyboardWillChange:(NSNotification *)notification | |
{ | |
[self.view layoutIfNeeded]; | |
NSDictionary* info = notification.userInfo; | |
CGRect kbRect = [info[UIKeyboardFrameEndUserInfoKey] CGRectValue]; | |
CGFloat fromBottom = MAX(0, self.view.frame.size.height - kbRect.origin.y); | |
NSValue * animationDurationValue = info[UIKeyboardAnimationDurationUserInfoKey]; | |
NSTimeInterval duration; | |
[animationDurationValue getValue:&duration]; | |
UIViewAnimationCurve curve = [info[UIKeyboardAnimationCurveUserInfoKey] integerValue]; | |
UIViewAnimationOptions animOpt; | |
switch (curve) { | |
case UIViewAnimationCurveEaseInOut: | |
animOpt = UIViewAnimationOptionCurveEaseInOut; | |
break; | |
case UIViewAnimationCurveEaseIn: | |
animOpt = UIViewAnimationOptionCurveEaseIn; | |
break; | |
case UIViewAnimationCurveEaseOut: | |
animOpt = UIViewAnimationOptionCurveEaseOut; | |
break; | |
case UIViewAnimationCurveLinear: | |
animOpt = UIViewAnimationOptionCurveLinear; | |
break; | |
default: | |
animOpt = UIViewAnimationOptionCurveEaseInOut; | |
break; | |
} | |
animOpt = animOpt | UIViewAnimationOptionTransitionNone; | |
void (^animation)() = ^ | |
{ | |
[self doKeyboardAnimateTo:fromBottom]; | |
[self.view layoutIfNeeded]; | |
}; | |
void (^completion)(BOOL) = ^(BOOL fin) | |
{ | |
[self completedKeyboardAnimateTo:fromBottom]; | |
}; | |
[UIView animateWithDuration:duration | |
delay:0 | |
options:animOpt | |
animations:animation | |
completion:completion]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment