失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > [iOS]利用通知实现监听系统键盘

[iOS]利用通知实现监听系统键盘

时间:2019-08-03 23:45:56

相关推荐

[iOS]利用通知实现监听系统键盘

//// ViewController.m// text//// Created by 李东旭 on 16/1/22.// Copyright © 李东旭. All rights reserved.//#import <UIKit/UIKit.h>#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];UITextView *textV = [[UITextView alloc] initWithFrame:self.view.bounds];[self.view addSubview:textV];// 1. 添加通知[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardChange:) name:UIKeyboardWillShowNotification object:nil];// 键盘将要出现 UIKeyboardWillShowNotification// 键盘已经出现 UIKeyboardDidShowNotification// 键盘将要隐藏 UIKeyboardWillHideNotification// 键盘已经隐藏 UIKeyboardDidHideNotification}// 2. 键盘已经出现调用的触发方法- (void)keyBoardChange:(NSNotification *)noti{NSLog(@"%@", noti);// 打印noti/*0x7f84584d1000 {name = UIKeyboardDidShowNotification; userInfo = {// 键盘动画执行节奏UIKeyboardAnimationCurveUserInfoKey = 7;// 键盘动画的时间UIKeyboardAnimationDurationUserInfoKey = "0.25";// 键盘的boundsUIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 258}}";// 键盘的起始centerUIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 796}";// 键盘的结束centerUIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 538}";// 键盘开始动画前的位置(也就是消失时)UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 258}}";// 键盘结束动画后的位置(也就是弹出了)UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 409}, {375, 258}}";UIKeyboardIsLocalUserInfoKey = 1;}}*/// 3. 那么如何获取这个值呢NSDictionary *noKey = [noti userInfo];NSValue *value = noKey[@"UIKeyboardBoundsUserInfoKey"];// 注意,这个的value 是CGRect类型的(所以下面必须调用CGRectValue方法NSLog(@"%f", [value CGRectValue].size.height);}@end

如果觉得《[iOS]利用通知实现监听系统键盘》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。