Skip to content

Instantly share code, notes, and snippets.

@jpiche
Last active November 23, 2015 23:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jpiche/a15e625368216eb0baa6 to your computer and use it in GitHub Desktop.
Save jpiche/a15e625368216eb0baa6 to your computer and use it in GitHub Desktop.
UIKeyboardWillChangeFrameNotification method
- (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];
}
@akashgupta88
Copy link

Thanks for posting this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment