ios - 使用键盘动画 UIView

标签 ios objective-c uikeyboard

<分区>

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

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

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

注册键盘通知:

[[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];
}

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

最佳答案

这对我有用:

- (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];
}

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

- (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) {
        ...
    }];
}

关于ios - 使用键盘动画 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24099318/

相关文章:

iOS,键盘预测栏可见性

ios - Jenkins IOS 构建和 Git 分支

objective-c - arc4random 和 % 运算符

javascript - 目标 ="_blank"在 IOS 中不工作

ios - 在 Objective-C 中使用全局变量

ios - applicationWillEnterForeground 要求重启 iphone?

ios - UIKeyboard 将通过交互式键盘关闭更改框架,而不是连续调用

ios - 在另一个位置显示 UIKeyboard iOS, iPad

iphone - 使用 iPhone 应用程序在 Google+ 中分享图片

ios - 按钮图像拉伸(stretch)