Skip to content

Instantly share code, notes, and snippets.

@bluebanboom
Created October 10, 2012 09:45
Show Gist options
  • Save bluebanboom/3864422 to your computer and use it in GitHub Desktop.
Save bluebanboom/3864422 to your computer and use it in GitHub Desktop.
Keyboard Show/Hidde Event
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGRect keyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
CGSize kbSize = keyboardRect.size;
// OPLog(@"keyboardRect: %@", NSStringFromCGRect(keyboardRect));
double duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
NSInteger curve = [[info objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
CGFloat width = self.view.bounds.size.width;
CGFloat heigth = self.view.bounds.size.height;
// OPLog(@"%.3f %d size: %@", duration, curve, NSStringFromCGSize(kbSize));
[UIView beginAnimations:@"OPKeyBoardShow" context:NULL];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
_commentContainerView.frame = CGRectMake(0, heigth - kbSize.height - _commentContainerView.frame.size.height, width, _commentContainerView.frame.size.height);
[UIView commitAnimations];
}
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
CGFloat width = self.view.bounds.size.width;
CGFloat height = self.view.bounds.size.height;
NSDictionary* info = [aNotification userInfo];
double duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
NSInteger curve = [[info objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
// OPLog(@"%.3f %d", duration, curve);
[UIView beginAnimations:@"OPKeyBoardHidde" context:NULL];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
_commentContainerView.frame = CGRectMake(0, height - _commentContainerView.frame.size.height, width, _commentContainerView.frame.size.height);
[UIView commitAnimations];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment