当我的键盘被激活时,我正在使用以下代码来移动视图和我的tableview.然而,当键盘关闭时,在键盘关闭后2秒内将upView固定为实时(另一方面,tableView是即时的).为什么会这样?
- (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,movement); [UIView setAnimationDidStopSelector:@selector(afteranimationStops)]; [UIView commitAnimations]; self.tableView.frame = CGRectOffset(self.tableView.frame,movement); [UIView setAnimationDidStopSelector:@selector(afteranimationStops)]; [UIView commitAnimations]; } - (void)textViewDidBeginEditing:(UITextView *)textView { [self animateTextView:YES]; } - (void)textViewDidEndEditing:(UITextView *)textView { [self animateTextView:NO]; }
更新的代码
.M
- (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,self.keyboardHeight); self.tableView.frame = CGRectOffset(self.tableView.frame,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,-self.keyboardHeight); self.tableView.frame = CGRectOffset(self.tableView.frame,-self.keyboardHeight); } completion:^(BOOL finished) {}]; [self moveCustomView:YES duration:duration]; } } - (void)moveCustomView:(BOOL)move duration:(NSTimeInterval)time{ }
解决方法
此问题可能与动画持续时间有关,因此您可以从 – (void)handleKeyboard:(NSNotification *)notification {}获取键盘显示和隐藏动画持续时间
并处理在同一个函数中显示和隐藏自定义视图.将以下代码添加到viewDidLoad函数中
[[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(handleKeyboard:) name:UIKeyboardWillHideNotification object:nil]; [[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(handleKeyboard:) name:UIKeyboardWillShowNotification object:nil];
处理键盘操作和UI更改
- (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{ }