博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
监听键盘弹出 隐藏
阅读量:4550 次
发布时间:2019-06-08

本文共 1485 字,大约阅读时间需要 4 分钟。

- (void)viewDidLoad {

    [super viewDidLoad];

 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

 

 

//隐藏键盘

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{
    [self.view endEditing:YES];
}
- (void)keyboardWillShow:(NSNotification *)notification {
    
    //改变window的背景颜色
    self.view.window.backgroundColor = self.view.backgroundColor;
    
    //  键盘退出的frame
    CGRect frame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    //键盘实时y
    CGFloat keyY = frame.origin.y;
    
    if (_nextBtny <= keyY-LTloginInset ) {
        
        return;
    }
    
    CGFloat screenh = keyY-_nextBtny-LTloginInset;
    
    //动画时间
    CGFloat keyDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
        //执行动画
    [UIView animateWithDuration:keyDuration animations:^{
        self.view.transform = CGAffineTransformMakeTranslation(0,screenh );
    }];
}
- (void)keyboardWillHide:(NSNotification *)notification {
    
    //动画时间
    CGFloat keyDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    //执行动画
    [UIView animateWithDuration:keyDuration animations:^{
        self.view.transform = CGAffineTransformMakeTranslation(0,0);
    }];

}

- (void)dealloc

{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:nil object:nil];
    
}

转载于:https://www.cnblogs.com/guangleijia/p/4759748.html

你可能感兴趣的文章
延迟初始化
查看>>
字符串格式化和format
查看>>
页面内容添加新的显示或者显示隐藏的内容,滚动条滚动到最低端,显示出新内容...
查看>>
【poj1182】 食物链
查看>>
Oracle学习之start with...connect by子句的用法
查看>>
matlab去云雾
查看>>
500lines项目简介
查看>>
Asp.net core logging 日志
查看>>
第十一章 类和对象[DDT书本学习 小甲鱼]【1】
查看>>
hdu2035 二分快速幂
查看>>
BOM浏览器对象模型
查看>>
python实现排序奇数在前偶数在后
查看>>
hdu 3368 曾经下过的棋
查看>>
Linux Shell 删除三天前文件
查看>>
微信小程序 使用mpvue
查看>>
常用模块-02
查看>>
接口测试总结
查看>>
测试的基本概念
查看>>
【ZOJ】3209 Treasure Map
查看>>
ActiveMQ 消息队列服务
查看>>