Posted By

BenClayton on 04/07/11


Tagged


Versions (?)

iPhone: Slide view up and down to match keyboard appearing and disappearing


 / Published in: Objective C
 

  1. - (void)viewDidLoad
  2. {
  3. [[NSNotificationCenter defaultCenter]
  4. addObserver:self
  5. selector:@selector(keyboardWillShow:)
  6. name:UIKeyboardWillShowNotification
  7. object:nil];
  8. [[NSNotificationCenter defaultCenter]
  9. addObserver:self
  10. selector:@selector(keyboardWillHide:)
  11. name:UIKeyboardWillHideNotification
  12. object:nil];
  13. }
  14.  
  15. - (void)dealloc
  16. {
  17. [[NSNotificationCenter defaultCenter] removeObserver:self];
  18. }
  19.  
  20. - (void)keyboardWillShow:(NSNotification *)notification {
  21. CGRect start, end;
  22.  
  23. // position of keyboard before animation
  24. [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&start];
  25. // and after..
  26. [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&end];
  27.  
  28. double duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
  29. int curve = [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
  30.  
  31. // slide view up..
  32. [UIView beginAnimations:@"foo" context:nil];
  33. [UIView setAnimationDuration:duration];
  34. [UIView setAnimationCurve:curve];
  35. self.view.frame = CGRectMake(0, -end.size.width, 480, 320);
  36. [UIView commitAnimations];
  37. }
  38.  
  39. - (void) keyboardWillHide:(NSNotification *)notification {
  40. double duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
  41. int curve = [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
  42.  
  43. // slide view down
  44. [UIView beginAnimations:@"foo" context:nil];
  45. [UIView setAnimationDuration:duration];
  46. [UIView setAnimationCurve:curve];
  47. self.view.frame = CGRectMake(0, 0, 480, 320);
  48. [UIView commitAnimations];
  49. }

Report this snippet  

You need to login to post a comment.