IOS 监听键盘的通知(NSNotificationCenter)
2017-02-28 20:04
405 查看
通知方法:
调用通知:
移出监听 ARC的情况
/** * 当键盘改变了frame(位置和尺寸)的时候调用 */ - (void)keyboardWillChangeFrame:(NSNotification *)note { // 设置窗口的颜色 self.view.window.backgroundColor = self.tableView.backgroundColor; // 0.取出键盘动画的时间 CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; // 1.取得键盘最后的frame CGRect keyboardFrame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; // 2.计算控制器的view需要平移的距离 CGFloat transformY = keyboardFrame.origin.y - self.view.frame.size.height; // 3.执行动画 [UIView animateWithDuration:duration animations:^{ self.view.transform = CGAffineTransformMakeTranslation(0, transformY); }]; } /* UIKeyboardAnimationCurveUserInfoKey = 7; // 动画的执行节奏(速度) UIKeyboardAnimationDurationUserInfoKey = "0.25"; // 键盘弹出\隐藏动画所需要的时间 UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}"; UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 588}"; UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 372}"; UIKeyboardFrameChangedByUserInteraction = 0; // 键盘弹出 UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 480}, {320, 216}}";// 键盘刚出来那一刻的frame UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 264}, {320, 216}}"; // 键盘显示完毕后的frame // 键盘隐藏 UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 264}, {320, 216}}"; UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 480}, {320, 216}}"; */
调用通知:
// 2.监听键盘的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
移出监听 ARC的情况
-(void)dealloc { //[super dealloc]; 非ARC的情况需要这一步 [[NSNotificationCenter defaultCenter]removeObserver:self]; }
相关文章推荐
- ios-day08-02(ios的通知机制:通知的发布、监听、移除,一些常见的系统通知、键盘状态的通知、通知和代理的异同)
- iOS开发(5)动态监听键盘通知
- ios-day08-01(模仿QQ聊天界面。ios通知的使用(对键盘状态的监听)、枚举和分类的使用、拉伸图片、计算文本字符串的尺寸
- [iOS]利用通知实现监听系统键盘
- [iOS]利用通知实现监听系统键盘
- iOS - 通知监听键盘高度
- iOS--利用通知监听键盘
- IOS学习笔记(8)UIPageViewController UIProgressView 监听和响应键盘通知
- IOS: iPhone键盘通知与键盘定制
- iphone ios 中监听、KVO、观察和通知的五种方法
- IOS: iPhone键盘通知与键盘定制
- iOS 监听键盘
- iphone ios 中监听、观察和通知的五种方法
- ios 键盘监听
- IOS学习笔记(8)UIPageViewController UIProgressView 监听和响应键盘通知
- ios键盘通知和自定义键盘
- iOS键盘中英文切换键盘高度获取通知方法
- ios 动态监听键盘输入法和高度
- iOS——tableView和适配、键盘弹出发送通知问题
- IOS: iPhone键盘通知与键盘定制