iOS开发——UI进阶篇(六)键盘处理
2015-07-26 23:59
519 查看
一、键盘通知
我们经常需要在键盘弹出或者隐藏的时候做一些特定的操作,因此需要监听键盘的状态
键盘状态改变的时候,系统会发出一些特定的通知
UIKeyboardWillShowNotification // 键盘即将显示
UIKeyboardDidShowNotification // 键盘显示完毕
UIKeyboardWillHideNotification // 键盘即将隐藏
UIKeyboardDidHideNotification // 键盘隐藏完毕
UIKeyboardWillChangeFrameNotification // 键盘的位置尺寸即将发生改变
UIKeyboardDidChangeFrameNotification // 键盘的位置尺寸改变完毕
系统发出键盘通知时,会附带一下跟键盘有关的额外信息(字典),字典常见的key如下:
UIKeyboardFrameBeginUserInfoKey // 键盘刚开始的frame
UIKeyboardFrameEndUserInfoKey // 键盘最终的frame(动画执行完毕后)
UIKeyboardAnimationDurationUserInfoKey // 键盘动画的时间
UIKeyboardAnimationCurveUserInfoKey // 键盘动画的执行节奏(快慢)
点击Text Field弹出文字时 让Text Field始终跟着键盘移动,并且贴着键盘上面
正因为键盘状态改变的时候,系统会发出一些特定的通知,我们可以监听键盘的状态
实现方法:
userInfo是系统提供的字典属性
@property (nullable, readonly, copy) NSDictionary *userInfo;
打印的结果如下:
UIKeyboardAnimationCurveUserInfoKey = 7; // 动画曲线动画
UIKeyboardAnimationDurationUserInfoKey = "0.25"; // 动画时间
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 253}}"; // 键盘bounds
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 694.5}"; // 开始键盘的居中位置
UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 441.5}"; // 结束键盘的居中位置
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 568}, {320, 253}}";// 键盘开始弹出的frame
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 315}, {320, 253}}";// 退出键盘的frame
UIKeyboardIsLocalUserInfoKey = 1;
我们经常需要在键盘弹出或者隐藏的时候做一些特定的操作,因此需要监听键盘的状态
键盘状态改变的时候,系统会发出一些特定的通知
UIKeyboardWillShowNotification // 键盘即将显示
UIKeyboardDidShowNotification // 键盘显示完毕
UIKeyboardWillHideNotification // 键盘即将隐藏
UIKeyboardDidHideNotification // 键盘隐藏完毕
UIKeyboardWillChangeFrameNotification // 键盘的位置尺寸即将发生改变
UIKeyboardDidChangeFrameNotification // 键盘的位置尺寸改变完毕
系统发出键盘通知时,会附带一下跟键盘有关的额外信息(字典),字典常见的key如下:
UIKeyboardFrameBeginUserInfoKey // 键盘刚开始的frame
UIKeyboardFrameEndUserInfoKey // 键盘最终的frame(动画执行完毕后)
UIKeyboardAnimationDurationUserInfoKey // 键盘动画的时间
UIKeyboardAnimationCurveUserInfoKey // 键盘动画的执行节奏(快慢)
点击Text Field弹出文字时 让Text Field始终跟着键盘移动,并且贴着键盘上面
正因为键盘状态改变的时候,系统会发出一些特定的通知,我们可以监听键盘的状态
- (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; }
实现方法:
/** * 监听键盘的frame即将发生改变的时候调用 */ - (void)keyboardWillChange:(NSNotification *)note { // 获得键盘最后的frame CGRect frame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; // 修改底部约束 self.bottomSpace.constant = self.view.frame.size.height - frame.origin.y; // 执行动画 CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; [UIView animateWithDuration:duration animations:^{ [self.view layoutIfNeeded]; }]; } // 触摸屏幕退出键盘 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // 文本框不再是第一响应者,就会退出键盘 // [self.textField resignFirstResponder]; // [self.textField endEditing:YES]; [self.view endEditing:YES]; }
userInfo是系统提供的字典属性
@property (nullable, readonly, copy) NSDictionary *userInfo;
打印的结果如下:
UIKeyboardAnimationCurveUserInfoKey = 7; // 动画曲线动画
UIKeyboardAnimationDurationUserInfoKey = "0.25"; // 动画时间
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 253}}"; // 键盘bounds
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 694.5}"; // 开始键盘的居中位置
UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 441.5}"; // 结束键盘的居中位置
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 568}, {320, 253}}";// 键盘开始弹出的frame
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 315}, {320, 253}}";// 退出键盘的frame
UIKeyboardIsLocalUserInfoKey = 1;
相关文章推荐
- UI-框架---快切
- Key-value存储简介
- html中类似 <input type=“text” name="username[]" value="1">中的方括号的含义
- Unique path ii
- Leetcode #60 Permutation Sequence
- MarqueeTextview(跑马灯)
- UIScrollView的属性总结
- UI___UIImageView
- php中break与continue的区别
- CDOJ 485 UESTC 485 Game (八数码变形,映射,逆cantor展开)
- UGUI下拉列表控件
- UICollectionView详解5:瀑布流
- iOS UI异步更新:dispatch_async 与 dispatch_get_global_queue 的使用方法
- iOS中的基本 UI 控件的使用
- UIButton之ImageEdge&TitleEdge笔记
- UIPickerView
- CDOJ 490 UESTC 490 Swap Game(思路,逆序对)
- EasyUI总结
- MIUI 7 会是小米的救命稻草吗?
- DirectUI界面编程(五)WindowImplBase的使用