I use the code below to change the view and my view of the table when my keyboard is activated. However, when the keyboard is closed, it takes 2 seconds after the keyboard is closed to return to where it was (on the other hand, instantly). Why is this happening?
- (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillShowNotification object:nil]; } - (void)keyboardWillChange:(NSNotification *)notification { NSDictionary* keyboardInfo = [notification userInfo]; NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey]; CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue]; UITabBarController *tabBarController = [UITabBarController new]; CGFloat tabBarHeight = tabBarController.tabBar.frame.size.height; self.keyboardHeight = keyboardFrameBeginRect.size.height - tabBarHeight; } - (void) animateTextView:(BOOL) up { const int movementDistance = self.keyboardHeight; const float movementDuration = 0.2f; int movement= movement = (up ? -movementDistance : movementDistance); [UIView beginAnimations: @"anim" context: nil]; [UIView setAnimationBeginsFromCurrentState: YES]; [UIView setAnimationDuration: movementDuration]; self.upView.frame = CGRectOffset(self.upView.frame, 0, movement); [UIView setAnimationDidStopSelector:@selector(afterAnimationStops)]; [UIView commitAnimations]; self.tableView.frame = CGRectOffset(self.tableView.frame, 0, movement); [UIView setAnimationDidStopSelector:@selector(afterAnimationStops)]; [UIView commitAnimations]; } - (void)textViewDidBeginEditing:(UITextView *)textView { [self animateTextView:YES]; } - (void)textViewDidEndEditing:(UITextView *)textView { [self animateTextView:NO]; }
UPDATED CODE
wow
- (void)handleKeyboard:(NSNotification*)aNotification{ NSDictionary* info = [aNotification userInfo]; NSValue* value = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey]; NSTimeInterval duration = 3; [value getValue:&duration]; if (aNotification.name == UIKeyboardWillHideNotification) { [UIView animateWithDuration:0 animations:^{ self.upView.frame = CGRectOffset(self.upView.frame, 0, self.keyboardHeight); self.tableView.frame = CGRectOffset(self.tableView.frame, 0, self.keyboardHeight); } completion:^(BOOL finished) {}]; [self moveCustomView:NO duration:duration]; NSLog(@"CLOSED!"); } if (aNotification.name == UIKeyboardWillShowNotification) { [UIView animateWithDuration:0 animations:^{ self.upView.frame = CGRectOffset(self.upView.frame, 0, -self.keyboardHeight); self.tableView.frame = CGRectOffset(self.tableView.frame, 0, -self.keyboardHeight); } completion:^(BOOL finished) {}]; [self moveCustomView:YES duration:duration]; } } - (void)moveCustomView:(BOOL)move duration:(NSTimeInterval)time{ }
source share