(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_chatView = [[UIView alloc] initWithFrame:CGRectMake(0, 440, 320, 40)];
_chatView.backgroundColor = [UIColor grayColor];
[self.view addSubview:_chatView];UITextField * f = [[UITextField alloc] initWithFrame:CGRectMake(20, 5, 240, 30)];
f.borderStyle = UITextBorderStyleRoundedRect;
[_chatView addSubview:f];UIButton * b = [UIButton buttonWithType:UIButtonTypeSystem];
[b setTitle:@”发送” forState:UIControlStateNormal];
b.frame = CGRectMake(270,5,40,30);
[_chatView addSubview:b];// NSNotificationCenter 通知中心
// 注册
/*
参数1:观察者对象
参数2:方法选择器
参数3:向通知中心注册的字符串
参数4:参数
向通知中心注册一个观察者对象
*/
// 当键盘即将抬起时 触发当前对象中的keyboardWillShow方法
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
// 必须在delloc中移除观察者身份
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];// UITextView 折行的UITextField
// 高仿 QQ聊天的下半部分界面
}(void)keyboardWillShow:(id)note
{
// NSLog(@”note is %@”,note);
// 动画持续时间 键盘高度
// userInfo = {
// UIKeyboardAnimationCurveUserInfoKey = 0;
// UIKeyboardAnimationDurationUserInfoKey = 0;
// UIKeyboardBoundsUserInfoKey = “NSRect: {{0, 0}, {320, 216}}”;
// UIKeyboardCenterBeginUserInfoKey = “NSPoint: {160, 353.5}”;
// UIKeyboardCenterEndUserInfoKey = “NSPoint: {160, 372}”;
// UIKeyboardFrameBeginUserInfoKey = “NSRect: {{0, 227}, {320, 253}}”;
// UIKeyboardFrameEndUserInfoKey = “NSRect: {{0, 264}, {320, 216}
// }// 获取键盘高度 高度为动态
// 字典中拿到的值是NSRect 转化为 CGRect 方法:CGRectValue
CGFloat height = [[[note userInfo] objectForKey:@”UIKeyboardFrameEndUserInfoKey”] CGRectValue].size.height;
// 动画调整_chatView的位置 与键盘同步
[UIView animateWithDuration:0.25 animations:^{
_chatView.frame = CGRectMake(0, 480-height-40, 320, 40);
}];
}(void)keyboardWillHidden:(NSNotification *)note
{
[UIView animateWithDuration:0.25 animations:^{
_chatView.frame = CGRectMake(0, 480-40, 320, 40);
}];
}(void)touchesEnded:(NSSet )touches withEvent:(UIEvent )event
{
[self.view endEditing:YES];
}
IOS键盘通知中心方法
最新推荐文章于 2020-12-19 12:59:44 发布