Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save logicaroma/909193 to your computer and use it in GitHub Desktop.
Save logicaroma/909193 to your computer and use it in GitHub Desktop.
Object-C iOS
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)keyboardWillShow:(NSNotification *)notification {
CGRect start, end;
// position of keyboard before animation
[[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&start];
// and after..
[[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&end];
double duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
int curve = [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
// slide view up..
[UIView beginAnimations:@"foo" context:nil];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
self.view.frame = CGRectMake(0, -end.size.width, 480, 320);
[UIView commitAnimations];
}
- (void) keyboardWillHide:(NSNotification *)notification {
double duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
int curve = [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
// slide view down
[UIView beginAnimations:@"foo" context:nil];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
self.view.frame = CGRectMake(0, 0, 480, 320);
[UIView commitAnimations];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment