iOS键盘延迟动画

问题描述:

我正在尝试为iOS应用程序开发聊天功能。我一直在玩键盘,除了动画以外,一切似乎都在起作用。iOS键盘延迟动画

当我按下键盘时,向上移动视图的动画会延迟一秒。我做了一个video of it happening here

我的代码如下:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil]; 

选择器:

- (void)keyboardDidShow: (NSNotification *) notif{ 
    NSDictionary *info = [notif userInfo]; 
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; 
    _currentKeyboardHeight = kbSize.height; 

    // Calculate free space between navigation bar and keyboard top to reposition the chat bubbles. 
    float areaHeight = screenHeight - (_currentKeyboardHeight + _footer.frame.size.height + 100); 

    if([Infrastructure_Connection ConnectivityCheck]){ 
     [UIView animateWithDuration:0.2 delay:0 options:0 animations:^{ 
      [_ChatBar setFrame:CGRectMake(posX, screenHeight-(_currentKeyboardHeight+height), width, height)]; 
      bubbleTableMain.frame = CGRectMake(0,100, self.view.frame.size.width,areaHeight); 
     } completion:^(BOOL finished) { 
      [bubbleTableMain scrollBubbleViewToBottomAnimated:YES]; 
     }]; 
    } else { 
    [RegisterUser AlertMessage:@"Offline" Message:@"You're currently in offline mode."]; 
    } 
} 


- (void)keyboardDidHide: (NSNotification *) notif{ 
    int whereKeyboardEnds = self.view.frame.size.height-(195); 
    [UIView animateWithDuration:0.2 delay:0.0 options:0 animations:^{ 
     bubbleTableMain.frame = CGRectMake(0,100, self.view.frame.size.width,whereKeyboardEnds); 
     [_ChatBar setFrame:CGRectMake(posX, 474, width, height)]; 
    } completion:^(BOOL finished) { }]; 
} 

的延迟是在0.0和仍然它通过第二延迟它。任何人都有任何想法,例如,Whatsapp如何让他们的键盘互动如此平稳,并随着键盘的出现而及时出现?

===================编辑===================

所以我已经改变了下面的代码,现在出于某种原因,它不运行动画。我已经调试过了,它正确地调用了选择器。但不动画:

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

- (void)keyboardWillShow:(NSNotification *)note { 
    UIViewAnimationCurve animationCurve = [[[note userInfo] valueForKey: UIKeyboardAnimationCurveUserInfoKey] intValue]; 
    NSTimeInterval animationDuration = [[[note userInfo] valueForKey: UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 
    [UIView beginAnimations:nil context: nil]; 
    [UIView setAnimationCurve:animationCurve]; 
    [UIView setAnimationDuration:animationDuration]; 
    [_ChatBar setFrame:CGRectMake(0, 10, _ChatBar.frame.size.width, _ChatBar.frame.size.height)]; 
    [UIView commitAnimations]; 
} 

您正在使用键盘完成其动画完成后,这就是所谓的keyboardDidShow通知。改为使用keyboardWillShow通知。这应该够了吧。

+0

嗨,谢谢你的回应!我改变了电话并更新了方法。但是现在动画不能运行?在调试中,它通过该方法运行,但不执行动画。我编辑了我的原始问题以显示更新的代码。它适用于UIKeyboardDidShowNotification,但不适用于UIKeyboardWillShowNotification。有任何想法吗? – Jahoe 2015-04-03 17:13:53

+0

你检查了animationCurve和animationDuration的值吗? – dominikweifieg 2015-04-04 09:57:34