iOS 监听键盘实现页面上滑下滑方法

       键盘弹起下落连带页面滑动效果大同小异.

       好吧.几句话.

       首先,在viewdidload里对键盘进行监听.

     

 //监听键盘

    [[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 *)n {

    //NSLog(@"%@",n.userInfo);

    //获得键盘升起的时间

    NSTimeInterval time = [[n.userInfo objectForKey:@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];

    //获得键盘升起的高度

    float height = [[n.userInfo objectForKey:@"UIKeyboardFrameEndUserInfoKey"] CGRectValue].size.height;


    [UIView animateWithDuration:time animations:^{

        CGRect frame = self.view.frame;

        frame.origin.y = -height;

        self.view.frame = frame;

        [self.view layoutIfNeeded];

    }];

}


//监听键盘隐藏的方法

- (void)keyBoardWillHide:(NSNotification *)n {

    NSTimeInterval time = [[n.userInfo objectForKey:@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];

    [UIView animateWithDuration:time animations:^{

        CGRect frame = self.view.frame;

        frame.origin.y = 0;

        self.view.frame = frame;

        [self.view layoutIfNeeded];

    }];

}

   

//最后打个广告,新建iOS技术群,欢迎大家加入进行技术讨论.群号:207577704

你可能感兴趣的:(iOS 监听键盘实现页面上滑下滑方法)