Obj-c - Delayed animation on reset?

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) { /** KEYBOARD HIDE **/ [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) { /** KEYBOARD SHOW **/ [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{ } 
+5
source share
2 answers

This problem may be related to the duration of the animation, so you can make the keyboard show and hide the duration of the animation from -(void)handleKeyboard:(NSNotification *)notification {}

as well as handle the show and hide your custom view inside the same function. Add the following code to your viewDidLoad function

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboard:) name:UIKeyboardWillHideNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboard:) name:UIKeyboardWillShowNotification object:nil]; 

Control keyboard actions and user interface changes

 - (void)handleKeyboard:(NSNotification*)aNotification{ NSDictionary* info = [aNotification userInfo]; NSValue* value = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey]; NSTimeInterval duration = 0; [value getValue:&duration]; if (aNotification.name == UIKeyboardWillHideNotification) { /** KEYBOARD HIDE **/ //calculate your view frames and handle UI changes /* . . . . . */ [self moveCustomView:NO duration:duration]; } if (aNotification.name == UIKeyboardWillShowNotification) { /** KEYBOARD SHOW **/ //calculate your view frames and handle UI changes /* . . . . . */ [self moveCustomView:YES duration:duration]; } } - (void)moveCustomView:(BOOL)move duration:(NSTimeInterval)time{ } 
+4
source

I think you need to use a thread to invoke the animation immediately ...

 - (void)textViewDidBeginEditing:(UITextView *)textView { dispatch_async(dispatch_get_main_queue(), ^{ [self animateTextView:YES]; }); } - (void)textViewDidEndEditing:(UITextView *)textView { dispatch_async(dispatch_get_main_queue(), ^{ [self animateTextView:NO]; }); } 

I do not really understand your problem, but I will try it because if we encounter such problems, dispatch_async () will solve it.

+1
source

Source: https://habr.com/ru/post/1275023/


All Articles