【问题标题】:Animate UIView Up With Keyboard [duplicate]使用键盘动画 UIView [重复]
【发布时间】:2014-06-07 16:32:56
【问题描述】:

我需要将聊天功能添加到即将进行的项目中。

与此同时,我一直在尝试实现我所期望的简单问题,即在屏幕底部有一个 UIView,里面有一个 UITextView,当用户点击 UITextView 时,它将与键盘一起动画.

我有它的工作,但不幸的是,键盘的动画稍微落后于它上面的视图。到目前为止,这是我的实现:

注册键盘通知:

[[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*)notification
{
    CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    NSInteger curve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];

    [UIView animateWithDuration:duration delay:0 options:curve animations:^{

        CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
        _chatViewBottomConstraint.constant = keyboardFrame.size.height;
        [self.view layoutIfNeeded];

    } completion:nil];
}

有没有其他人做过类似的,并且能够为我提供更好的解决方案?

【问题讨论】:

    标签: ios objective-c uikeyboard


    【解决方案1】:

    这对我有用:

    - (void)keyboardWillShow:(id)keyboardDidShow
    {
        [UIView beginAnimations:nil context:NULL];
    
        NSDictionary *userInfo = [keyboardDidShow userInfo];
        [UIView setAnimationDuration:[userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
        [UIView setAnimationCurve:[userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]];
        [self.view layoutIfNeeded];
    
        [UIView commitAnimations];
    }
    
    - (void)keyboardWillHide:(id)keyboardDidHide
    {
        [UIView beginAnimations:nil context:NULL];
    
        NSDictionary *userInfo = [keyboardDidHide userInfo];
        [UIView setAnimationDuration:[userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
        [UIView setAnimationCurve:[userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]];            
        [self.view layoutIfNeeded];
    
        [UIView commitAnimations];
    }
    

    更新: 或者你可以对块做同样的事情:

    - (void)keyboardWillShow:(id)keyboardDidShow
    {
        NSDictionary *userInfo = [keyboardDidShow userInfo];
        [UIView animateWithDuration:[userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]
                              delay:0.f
                            options:[[keyboardDidShow userInfo][UIKeyboardAnimationCurveUserInfoKey] intValue] << 16
                         animations:^{
            ...
        } completion:^(BOOL finished) {
            ...
        }];
    }
    
    - (void)keyboardWillHide:(id)keyboardDidHide
    {
        NSDictionary *userInfo = [keyboardDidHide userInfo];
        [UIView animateWithDuration:[userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]
                              delay:0.f
                            options:[[keyboardDidHide userInfo][UIKeyboardAnimationCurveUserInfoKey] intValue] << 16
                         animations:^{
            ...
        } completion:^(BOOL finished) {
            ...
        }];
    }
    

    【讨论】:

    • 你为什么使用不推荐使用的动画方法而不是基于块的语法?
    • 好吧,我还在用 Xcode5。由于我目前仍在为 iOS7 开发。它没有将代码显示为已弃用。但是我已经用块更新了我的答案,我想这就是你的意思;)
    • 抱歉,我猜它们并没有被弃用,但自 iOS 4 以来,它们一直被“劝阻”(根据文档)。
    • 使用 blocks 方法似乎是导致我键盘和视图到达目的地之间延迟的问题。如果我使用动画方法,它工作正常。有任何想法吗?在动画块内我有: _chatViewBottomConstraint.constant = keyboardFrame.size.height; [self.view layoutIfNeeded];
    • Ben 的代码中有一个错误;曲线不能直接传递给选项。见:stackoverflow.com/questions/7327249/…
    【解决方案2】:

    通知 userInfo 字典有动画时长(UIKeyboardAnimationDurationUserInfoKey)和曲线(UIKeyboardAnimationCurveUserInfoKey)信息;如果您同时使用两者,您的动画应该与键盘动画的时间相匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-13
      • 2018-12-06
      • 1970-01-01
      • 1970-01-01
      • 2011-08-08
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      相关资源
      最近更新 更多