(二十四)监听键盘的通知和键盘弹出隐藏的View移动
来源:互联网 发布:linux java环境搭建 编辑:程序博客网 时间:2024/06/05 02:12
让控制器监听键盘的通知,注意谁监听,谁的dealloc方法中就要remove,如果非ARC还要调用父类的dealloc方法。
//监听键盘的操作: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
- (void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self];}- (void)keyboardWillChangeFrame:(NSNotification *)note{ NSLog(@"发送者%@ 内容%@",note.name,note.userInfo);}当键盘弹出时,接收到的内容为一个字典:需要注意的是其中的Key都有定义好的NSString,可以直接使用。
{ UIKeyboardAnimationCurveUserInfoKey = 7; //动画的执行节奏 UIKeyboardAnimationDurationUserInfoKey = "0.25"; //动画时长 UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 224}}"; UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 592}"; UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 368}"; UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 480}, {320, 224}}"; //键盘的起始位置、尺寸 UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 256}, {320, 224}}"; //键盘的结束位置、尺寸}
键盘退出的动作:滑动tableView实现键盘退出:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ //退出键盘 [self.view endEditing:YES];}
键盘动作时整个View跟着键盘走:
一个细节:由于字典中存放的都是对象,因此字典里的CGRect是封装以后的,用CGRectValue方法解开才是CGRect。
//注意,字典里存放的都是对象,要把对象转为CGRect结构体,使用CGRectValue方法。CGRect InfoKey = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];View随键盘移动的方法实现:注意对象向结构体的解包操作,注意note的成员,有userInfo和Object,二者是分开的。这样设计的视图还会随着键盘的尺寸实时变化。
细节:使用Transform可以方便的实现视图的移动。
- (void)keyboardWillChangeFrame:(NSNotification *)note{ //注意,字典里存放的都是对象,要把对象转为CGRect结构体,使用CGRectValue方法。 CGRect keyboardFrame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue]; CGFloat offsetY = keyboardFrame.origin.y - self.view.frame.size.height; [UIView animateWithDuration:duration animations:^{ self.view.transform = CGAffineTransformMakeTranslation(0, offsetY); }]; }不够协调的时候会看到黑色的原因:
视图控制器在创建时在最底层会有一个Window,默认为黑色。
解决办法之一是改变window的颜色。
AppDelegate中声明了window。
也可以直接在控制器里面改:self.view.window.backgroundColor可以设置窗口颜色,最好和TableView同色。
0 0
- (二十四)监听键盘的通知和键盘弹出隐藏的View移动
- 监听键盘的显示和隐藏 通知
- 监听键盘的弹出和隐藏
- 监听键盘的弹出与隐藏(界面跟随键盘一起上下移动)
- 键盘的弹出和隐藏
- 键盘的弹出和隐藏
- iOS开发监听键盘(添加键盘和隐藏键盘,改变视图中整体控件随着键盘的高度移动)
- 键盘弹出和消失View的上下移动效果
- 监听键盘弹出/隐藏
- 键盘弹出和收起的通知
- Android监听键盘的显示和隐藏
- android 监听键盘的弹起和隐藏
- 监听键盘弹出和隐藏,得到键盘高度
- 键盘的弹出与消失(通知)
- 通知的使用和通知对键盘的监听
- 键盘的通知 文本框跟随键盘移动
- IOS开发键盘上方的view随着键盘的弹出、收起、键盘输入法改变而移动
- 对弹出键盘的监听
- C#下的矩阵算法类
- jQuery弹性模态窗口导航菜单
- SDUT 1103 Quicksum
- 2.7学习内容 listview中item焦点获取顺序设置
- 比较两个UIImage是否相等
- (二十四)监听键盘的通知和键盘弹出隐藏的View移动
- android 透明布局点击穿透的处理
- Git使用手册:为Git仓库创建Submodule
- SDUT 1104 Image Transformation
- 为什么unix/linux/ubuntu比windows高级(国外专家解答)
- [LeetCode] Find Peak Element
- <分布式程序设计> 读书笔记一
- AppleStore视频预览制作
- SDUT 1126 软件工程