iOS 监听键盘事件

注册通知监听器,监听键盘弹起事件

    [[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 *)notificationP{
    //获取键盘弹出的高度
    NSDictionary *dict = [notificationP userInfo];
    NSValue *value = [dict objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect key = [value CGRectValue];
    NSLog(@"%lf",key.size.height);
}

键盘收起调用该方法

- (void)keyboardWillHide:(NSNotification *)notificationP{
     
}

开始视图升起动画

 [UIView beginAnimations:@"keyboardWillShow" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//获取原视图位置
CGRect rect = viewHua.frame;
rect.origin.y = 60;
//设置视图位置
viewHua.frame = rect;
//结束动画
[UIView commitAnimations];

开始视图下降动画

[UIView beginAnimations:@"keyboardWillHide" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//获取原视图位置
CGRect rect = viewHua.frame;
rect.origin.y = HEIGHT-250;
//设置视图位置
viewHua.frame = rect;
//结束动画
[UIView commitAnimations];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,625评论 25 709
  • 翻译自“View Controller Programming Guide for iOS”。 1 弹出视图控制器...
    lakerszhy阅读 8,993评论 2 20
  • 我是从什么时候开始对文字感兴趣的?仔细回想了一下,应该是初二。那时候写了一篇作文“我的同桌——睡神”被语文老师选为...
    左芷阅读 2,498评论 15 3
  • 刚看到魔戒中 法拉墨说的话 I did what I judged to be right(我做我认为对的事) 很...
    鲍鲍爱吃鱼阅读 1,436评论 0 0
  • 选择在你 我的同事豆豆是个不算很美但长相又有点小味道的女孩子,白白的皮肤,一头乌黑齐腰的长发让我羡慕不已,虽然有点...
    匹诺曹糖nana阅读 1,410评论 0 0