1.什么是通知?
系统帮助两个对象完成消息传递的一种机制.与代理模式的发消息的区别是:代理模式中A对象明确持有B对象的引用,然后A只能给B对象发消息,但是在通知中,引入了一个第三方,叫做通知中心的角色,A对象并不知道要给谁发消息,而是把消息交给通知中心,由通知中心将消息发给订阅了消息的B对象或者是更多地订阅了消息的对象
2.弹起键盘
//增加键盘的弹起通知监听
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(openKeyboard:) name:UIKeyboardWillShowNotification object:nil];
// 有键盘弹起,此方法就会被自动执行
-(void)openKeyboard:(NSNotification *)noti
{
// 获取键盘的 frame 数据
CGRect keyboardFrame = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
// 获取键盘动画的种类
int options = [noti.userInfo[UIKeyboardAnimationCurveUserInfoKey] intValue];
// 获取键盘动画的时长
NSTimeInterval duration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 修改文本框底部的约束的值
self.bottomConstraint.constant = keyboardFrame.size.height;
// 在动画内调用 layoutIfNeeded 方法
[UIView animateWithDuration:duration delay:0 options:options animations:^{
[self.view layoutIfNeeded];
} completion:nil];
}
3.收起键盘
//增加键盘的收起通知监听
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(closeKeyboard:) name:UIKeyboardWillHideNotification object:nil];
-(void)closeKeyboard:(NSNotification *)noti
{
// 获取键盘动画的种类
int options = [noti.userInfo[UIKeyboardAnimationCurveUserInfoKey] intValue];
// 获取键盘动画的时长
NSTimeInterval duration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
}