键盘弹出时视图的上移效果

想要完成这个功能,首先得知道键盘的弹出和隐藏是有系统通知的.

    UIKIT_EXTERN NSNotificationName const UIKeyboardWillShowNotification __TVOS_PROHIBITED;
    UIKIT_EXTERN NSNotificationName const UIKeyboardDidShowNotification __TVOS_PROHIBITED;
    UIKIT_EXTERN NSNotificationName const UIKeyboardWillHideNotification __TVOS_PROHIBITED;
    UIKIT_EXTERN NSNotificationName const UIKeyboardDidHideNotification __TVOS_PROHIBITED;
    UIKIT_EXTERN NSNotificationName const UIKeyboardWillChangeFrameNotification  NS_AVAILABLE_IOS(5_0) __TVOS_PROHIBITED;
    UIKIT_EXTERN NSNotificationName const UIKeyboardDidChangeFrameNotification   NS_AVAILABLE_IOS(5_0) __TVOS_PROHIBITED;

这几个通知一目了然吧,然后当然就是在这几个通知里做文章了.因为是系统来发送通知,我们只需要注册和注销观察者.注册一般就在要上移的视图的initWithFrame:方法中,然后注销是在视图销毁时.

    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
//注销观察者
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

当然,也可以注册UIKeyboardWillChangeFrameNotification通知,这个只需要注册一次就可以了,但是我怕这个里面会有我不知道的变化,所以放弃了.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];

然后就是对通知的利用了,我们需要知道NSNotification中有一个userInfo的字典,包含着键盘的各种数据,如下:

/**
 *  {
 *      name = UIKeyboardWillChangeFrameNotification;                               通知名称
 *      userInfo =                                                                  userInfo字典
 *      {
 *          UIKeyboardAnimationCurveUserInfoKey = 7;                                定义键盘弹出和隐藏动画的一个NSNumber类型的包含UIViewAnimationCurve的参数
 *          UIKeyboardAnimationDurationUserInfoKey = "0.25";                        动画时间
 *          UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 282}}";           键盘的bounds
 *          UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 427}";               键盘弹出和隐藏动画开始时键盘的中点
 *          UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 709}";                 键盘弹出和隐藏动画结束时键盘的中点
 *          UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 286}, {320, 282}}";     键盘弹出和隐藏动画开始时键盘的frame
 *          UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 568}, {320, 282}}";       键盘弹出和隐藏动画结束时键盘的frame
 *          UIKeyboardIsLocalUserInfoKey = 1;                                       一个标识键盘是否属于当前的应用程序,一般用于iPad多任务处理
 *      }
 *  }
 */

接下来就很简单了

{
    //取出动画结束时键盘的frame
    CGRect keyBoardFrame = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    //取出动画事件
    NSTimeInterval animationTime = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    //定义视图动画效果前后的frame
    CGRect oldFrame = self.frame;//视图动画前的frame
    CGRect newFrame;//视图动画前的frame
    //判断键盘是否遮盖了视图,如果是,那么上移视图;如果不是,那么不移动视图
    if (oldFrame.origin.y + oldFrame.size.height > keyBoardFrame.origin.y)
    {//视图被键盘遮盖
        newFrame = CGRectMake(oldFrame.origin.x, keyBoardFrame.origin.y - 64 - oldFrame.size.height, oldFrame.size.width, oldFrame.size.height);
    }
    else
    {//视图没有被键盘遮盖
        newFrame = self.frame;
    }
    
    [UIView animateWithDuration:animationTime animations:^{
        self.frame = newFrame;
    }];
}

自信满满的我运行了程序,然而根本没有用,没有上移效果...

打印下各自的frame

    NSLog(@"%@",NSStringFromCGRect(keyBoardFrame));
    NSLog(@"%@",NSStringFromCGRect(self.frame));
    
    2017-09-12 11:34:24.175458+0800 XXXXXXXXXX[7057:1778003] {{0, 286}, {320, 282}}
    2017-09-12 11:34:24.175501+0800 XXXXXXXXXX[7057:1778003] {{10, 10}, {300, 258.5}}

我发现不对球啊,明明被遮盖住了,为毛从数据上看起来没有被遮盖.想了会,我一拍屁股,这他么不是一个坐标系啊.对比一下,发现键盘是直接添加到UIWindow上面的,所以需要加一个判断,看视图的父控件的frame.origin.y是0还是64.最终代码如下:

- (void)changeFrameWith:(NSNotification *)notification
{
    //取出动画结束时键盘的frame
    CGRect keyBoardFrame = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    //取出动画事件
    NSTimeInterval animationTime = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    //定义视图动画效果前后的frame
    CGRect oldFrame = self.frame;//视图动画前的frame
    CGRect newFrame;//视图动画前的frame
    
    //看视图的父控件的frame.origin.y是0还是64.
    if (self.superview.frame.origin.y == 64)
    {
        //判断键盘是否遮盖了视图,如果是,那么上移视图;如果不是,那么不移动视图
        if (oldFrame.origin.y + oldFrame.size.height > keyBoardFrame.origin.y - 64)
        {//视图被键盘遮盖
            newFrame = CGRectMake(oldFrame.origin.x, keyBoardFrame.origin.y - 64 - oldFrame.size.height, oldFrame.size.width, oldFrame.size.height);
        }
        else
        {//视图没有被键盘遮盖
            newFrame = self.frame;
        }
    }
    else
    {
        //判断键盘是否遮盖了视图,如果是,那么上移视图;如果不是,那么不移动视图
        if (oldFrame.origin.y + oldFrame.size.height > keyBoardFrame.origin.y)
        {//视图被键盘遮盖
            newFrame = CGRectMake(oldFrame.origin.x, keyBoardFrame.origin.y - oldFrame.size.height, oldFrame.size.width, oldFrame.size.height);
        }
        else
        {//视图没有被键盘遮盖
            newFrame = self.frame;
        }
    }
    //添加动画效果
    [UIView animateWithDuration:animationTime animations:^{
        self.frame = newFrame;
    }];
}

你可能感兴趣的:(键盘弹出时视图的上移效果)