您的位置:首页 > 移动开发 > IOS开发

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

2016-02-25 14:09 453 查看
       键盘弹起下落连带页面滑动效果大同小异.

       好吧.几句话.

       首先,在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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: