当前位置: 首页 > news >正文

iOS边练边学--通知机制和键盘处理小练习

一、发送通知

1 NSNotification *note = [NSNotification notificationWithName:@"通知的名称,随便写,例如:你妈叫你回家吃饭" object:self userInfo:@{@"time":@"11:30",
2         @"desc":@"回家吃饭"
3                         }];
4     NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
5     [center postNotification:note];

二、接收通知:

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

三、键盘处理小练习

  注意事项:tableView的高度约束取消,如果约束在的话,tableView无法实现上移,只能实现往上压缩

  正确做法:设置tableView的高度约束等于父类约束的高度减去最下面工具栏的高度,如图:

  <1>将键盘的显示和隐藏分开处理的情况

  接收通知的代码:

1 - (void)viewDidLoad {
2     [super viewDidLoad];
3     // addObserver-谁来接收 @Selector-接收完成需要执行的方法,带一个参数,接收通知中心发来的NSNotofication对象
4     // object-发送消息的对象,nil-不管谁发送的,只要是name中的消息都接收,这里监听的是键盘的即将弹出的消息,系统发出的消息是字符串常亮
5     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
6     // 接收键盘隐藏的通知
7     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
8 }

  接收到通知后的处理代码:

 1 - (void)keyboardWillShow:(NSNotification *)note
 2 {
 3     // 取出时间
 4     CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
 5     // 取出键盘显示出来后的最终frame
 6     CGRect rect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
 7     // 改变约束
 8     self.bottomSpacing.constant = rect.size.height;
 9     // 动画效果
10     [UIView animateWithDuration:duration animations:^{
11         [self.view layoutIfNeeded];
12     }];
13 }
14 
15 - (void)keyboardWillHide:(NSNotification *)note
16 {
17     // 取出时间
18     CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
19     // 约束改为0
20     self.bottomSpacing.constant = 0;
21     [UIView animateWithDuration:duration animations:^{
22         [self.view layoutIfNeeded];
23     }];
24 }

  <2>将键盘显示和隐藏一起处理的情况

- (void)viewDidLoad {
    [super viewDidLoad];
    // 接收通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

 

 1 - (void)keyboardChange:(NSNotification *)note
 2 {
 3     // 取出时间
 4     CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
 5     // 取出键盘最终的frame
 6     CGRect rect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
 7     self.bottomSpacing.constant = [UIScreen mainScreen].bounds.size.height - rect.origin.y;
 8     [UIView animateWithDuration:duration animations:^{
 9         [self.view layoutIfNeeded];
10     }];
11 }
 1 - (void)keyboardChange:(NSNotification *)note
 2 {
 3     // 取出时间
 4     CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
 5     // 取出键盘最终的frame
 6     CGRect rect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
 7     self.bottomSpacing.constant = [UIScreen mainScreen].bounds.size.height - rect.origin.y;
 8     [UIView animateWithDuration:duration animations:^{
 9         [self.view layoutIfNeeded];
10     }];
11 }

四、最后一步,也是最重要的一步,记得取消注册的通知监听器

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

 

相关文章:

  • PHP CodeBase: 生成N个不重复的随机数
  • 解析stm32的时钟
  • BZOJ 1001 狼抓兔子 (网络流最小割/平面图的对偶图的最短路)
  • Material Design 控件
  • ARCproject中加入非ARC文件,或者非ARC环境中加入ARC文件
  • IOS开发UI篇--IOS动画(Core Animation)总结
  • css中的单位
  • 关于退运美国转基因玉米含有MRI 162转基因成分的质疑
  • Shiro基于组织机构的登录验证
  • maven部署构建到私服
  • Android 发送短信总结
  • 笔记 - 10.4、HTML - CSS滤镜笔记
  • Java BigDecimal详解
  • javap的使用
  • java面向对象中的方法重载与方法重写的区别
  • python3.6+scrapy+mysql 爬虫实战
  • 2017-09-12 前端日报
  • android百种动画侧滑库、步骤视图、TextView效果、社交、搜房、K线图等源码
  • Angular6错误 Service: No provider for Renderer2
  • JavaScript 事件——“事件类型”中“HTML5事件”的注意要点
  • Laravel Telescope:优雅的应用调试工具
  • laravel with 查询列表限制条数
  • node学习系列之简单文件上传
  • oschina
  • Vue ES6 Jade Scss Webpack Gulp
  • 工作踩坑系列——https访问遇到“已阻止载入混合活动内容”
  • 记录一下第一次使用npm
  • 记一次用 NodeJs 实现模拟登录的思路
  • 今年的LC3大会没了?
  • 前端技术周刊 2018-12-10:前端自动化测试
  • 数组大概知多少
  • 写给高年级小学生看的《Bash 指南》
  • 优秀架构师必须掌握的架构思维
  • media数据库操作,可以进行增删改查,实现回收站,隐私照片功能 SharedPreferences存储地址:
  • Spark2.4.0源码分析之WorldCount 默认shuffling并行度为200(九) ...
  • ​一文看懂数据清洗:缺失值、异常值和重复值的处理
  • # 睡眠3秒_床上这样睡觉的人,睡眠质量多半不好
  • (2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (22)C#传智:复习,多态虚方法抽象类接口,静态类,String与StringBuilder,集合泛型List与Dictionary,文件类,结构与类的区别
  • (pojstep1.3.1)1017(构造法模拟)
  • (二十三)Flask之高频面试点
  • (附源码)springboot美食分享系统 毕业设计 612231
  • (蓝桥杯每日一题)平方末尾及补充(常用的字符串函数功能)
  • (原)Matlab的svmtrain和svmclassify
  • (原创)Stanford Machine Learning (by Andrew NG) --- (week 9) Anomaly DetectionRecommender Systems...
  • (转)EOS中账户、钱包和密钥的关系
  • (转)GCC在C语言中内嵌汇编 asm __volatile__
  • (转载)hibernate缓存
  • ./configure,make,make install的作用(转)
  • .gitattributes 文件
  • .NET Core 控制台程序读 appsettings.json 、注依赖、配日志、设 IOptions
  • .net framwork4.6操作MySQL报错Character set ‘utf8mb3‘ is not supported 解决方法
  • .netcore 如何获取系统中所有session_ASP.NET Core如何解决分布式Session一致性问题
  • .net利用SQLBulkCopy进行数据库之间的大批量数据传递
  • .NET项目中存在多个web.config文件时的加载顺序