Advertisement
Guest User

keyboardAnimations

a guest
Jan 23rd, 2015
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -(void) keyboardWillShow: (NSNotification*)notification
  2. {
  3.     [self moveControls:notification up:YES];
  4. }
  5.  
  6. -(void) keyboardWillHide: (NSNotification*)notification
  7. {
  8.     [self moveControls:notification up:NO];
  9. }
  10.  
  11. -(void) moveControls:(NSNotification*)notification up:(BOOL)up
  12. {
  13.     NSDictionary* userInfo = [notification userInfo];
  14.     CGRect newFrame = [self getNewControlFrame:userInfo up:up];
  15.    
  16.     [self animateControls:userInfo withFrame:newFrame];
  17. }
  18.  
  19. -(CGRect)getNewControlFrame:(NSDictionary*)userInfo up:(BOOL)up
  20. {
  21.     CGRect kbFrame = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
  22.     kbFrame = [self.view convertRect:kbFrame fromView:nil];
  23.    
  24.     CGRect newFrame = self.scrollView.frame;
  25.     newFrame.origin.y += kbFrame.size.height * (up ? -1 : 1);
  26.    
  27.     return newFrame;
  28. }
  29.  
  30. -(void)animateControls:(NSDictionary*)userInfo withFrame:(CGRect)newFrame
  31. {
  32.     NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
  33.     UIViewAnimationCurve animationCurve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
  34.     [UIView animateWithDuration:duration
  35.                           delay:0
  36.                         options:animationOptionsWithCurve(animationCurve)
  37.                      animations:^{
  38.                             self.scrollView.frame = newFrame;
  39.                         }
  40.                      completion:^(BOOL finished) {}
  41.      ];
  42.    
  43.    
  44. }
  45.  
  46. static inline UIViewAnimationOptions animationOptionsWithCurve(UIViewAnimationCurve curve)
  47. {
  48.     return (UIViewAnimationOptions) curve << 16;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement