用键盘动画UIView [英] Animate UIView Up With Keyboard

查看:82
本文介绍了用键盘动画UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a requirement to add chat functionality into an upcoming project.

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

In the meantime I have been trying to implement what I expected to be the simple matter of having a UIView at the bottom of the screen which has a UITextView inside, which will animate up with the keyboard when the user taps on the UITextView.

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

I have it working, however unfortunately the animation for the keyboard is slightly behind that of the view above it. Here is my implementation so far:

注册键盘通知:

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

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

Has anyone else done similar, and are able to provide a better solution for me?

推荐答案

这对我有用:

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

更新:
或者你可以使用块执行相同的操作:

UPDATE: Or you can do the same with blocks:

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

这篇关于用键盘动画UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆