iOS键盘延迟动画

时间:2015-04-02 18:58:13

标签: ios iphone xcode uitextview uitoolbar

我目前正在尝试为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 个答案:

答案 0 :(得分:1)

您正在使用键盘完成动画后调用的keyboardDidShow通知。请改用keyboardWillShow通知。这应该可以解决问题。