您的位置:首页 > 移动开发 > IOS开发

键盘的弹出与消失(通知)

2016-02-05 11:51 483 查看
**键盘的通知
:**

//UIKeyboardWillShowNotification(键盘即将显示)

//UIKeyboardDidShowNotification(键盘已经显示)

//UIKeyboardWillHideNotification(键盘即将隐藏)

//UIKeyboardDidHideNotification(键盘已经隐藏)

//UIKeyboardWillChangeFrameNotification(键盘的位置尺寸即将发生 改变)

//UIKeyboardDidChangeFrameNotification(键盘的位置尺寸已经发生 改变)

1.订阅通知键盘的位置尺寸即将发生 改变

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];


2.例 键盘弹出 弹回

- (void)keyboardWillChangeFrame:(NSNotification *)noti
{
CGFloat duration = [noti.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];
CGRect frame = [noti.userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
CGFloat offsetY = frame.origin.y - self.view.frame.size.height;

[UIView animateWithDuration:duration animations:^{
self.view.transform = CGAffineTransformMakeTranslation(0, offsetY);
}];
}


最后不要忘了退订通知

- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios 通知中心 键盘