2015-04-02 202 views
0

我正在嘗試爲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]; 
} 

回答

1

您正在使用鍵盤完成其動畫完成後,這就是所謂的keyboardDidShow通知。改爲使用keyboardWillShow通知。這應該夠了吧。

+0

嗨,謝謝你的迴應!我改變了電話並更新了方法。但是現在動畫不能運行?在調試中,它通過該方法運行,但不執行動畫。我編輯了我的原始問題以顯示更新的代碼。它適用於UIKeyboardDidShowNotification,但不適用於UIKeyboardWillShowNotification。有任何想法嗎? – Jahoe 2015-04-03 17:13:53

+0

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