//***************************************************************************** -8通知中心
//-键盘位置改变的通知
//项目名称:QQ
//点击Text Field 文本框会回到键盘的上方 就可以显示自己输入的信息。
// 注意点 :这个textfield 中 小view 中
//键盘的通知
//UIKeyboardWillShowNotification (键盘即将显示)
//UIKeyboardDidShowNotification (键盘已经显示)
//UIKeyboardWillHideNotification (键盘即将隐藏)
//UIKeyboardDidHideNotification(键盘已经隐藏)
//UIKeyboardWillChangeFrameNotification(键盘的尺寸即将发生改变)
//UIKeyboardDidChangeFrameNotification (键盘的尺寸已经发生改变)
// UIKeyboardAnimationCurveUserInfoKey = 7;
// UIKeyboardAnimationDurationUserInfoKey = "0.25"; 动画持续的时间
// UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 258}}";键盘的大小
// UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 796}"; 中心点的坐标
// UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 538}";结束动画中心点的坐标
// UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 258}}";
// UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 409}, {375, 258}}";结束以后你的位置和大小
// }
- (void)viewDidLoad {
[super viewDidLoad];
//订阅通知
// addObserver 订阅者 name 通知的名称 object 谁发的通知 (selector) 方法
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
//键盘的位置或大小发生改变
- (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学习之——通知中心NSNotificationCenter
最新推荐文章于 2024-09-05 21:10:43 发布