一.通知
每一个应用程序都有一个通知中心,专门负责不同对象之间的消息通信,任何一个对象度可以向通知中心发布通知,其他感兴趣的对象可以申请在某一个特定的通知发布的时候(或者在某一个特定的对象发布通知时)接受这个通知. NSNotificationCenter 接受用户的注册,有通知过来时,用户就接收到了通知消息);
所有的通知都封装在NSNotification中;
1. 注册监听
addObserver : 监听者
selector : 接收到通知的时候,调用监听者的方法
name : 通知的名称
object :通知的发布者
注意:如果name 和object同时为空,那么接收所有的通知;一般请情况下name 不为空;
如果name 为空,接收某一个发布者的所有通知;
如果 object为空,接受名称为name 的所有通知;
[[NSNotificationCenterdefaultCenter]addObserver:zhangsan
selector:@selector(reciveNotification:)
name:@"Hunter"
object:souhuHunter];
2. 发布通知
postNotificationNam :必须和注册的时候name 保持一致
object :消息的发布者
userInfo :自定义的消息
[[NSNotificationCenterdefaultCenter] postNotificationName:@"Hunter"
object:souhuHunter
userInfo:@{@"company":souhuHunter}];
3.当监听者对象被销毁的时候,一定要从通知中心吧监听者移除掉
- (void)dealloc {
// 一定要把监听者移除掉
[[NSNotificationCenterdefaultCenter] removeObserver:self];
}
二.
- (void)viewDidLoad {
[superviewDidLoad];
// 设置控制器成为tableView的数据源代理
_tableView.dataSource =self;
// 设置控制器成为tableView的代理
_tableView.delegate =self;
// 每一个cell的高度都是相同
// _tableView.rowHeight = 60;
// 设置 textField 的代理为 控制器
_textField.delegate =self;
[selfregisterNotification];
}
#pragma mark -
#pragma mark - 添加对键盘的监听
- (void)registerNotification {
// 添加了一个 键盘即将显示时的监听,如果接收到通知,将调用 keyboardWillApprear:
[[NSNotificationCenterdefaultCenter]addObserver:self
selector:@selector(keyboardWillApprear:)
name:UIKeyboardWillShowNotification
object:nil];
// 添加监听, 键盘即将隐藏的时候,调用
[[NSNotificationCenterdefaultCenter]addObserver:self
selector:@selector(keyboardWillDisAppear:)
name:UIKeyboardWillHideNotification
object:nil];
}
#pragma mark -
#pragma mark - 键盘即将显示的时候调用
- (void)keyboardWillApprear:(NSNotification *)noti {
/**
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}}"; 弹出键盘之后的位置
UIKeyboardIsLocalUserInfoKey = 1;
*/
// 取出通知中的信息
NSDictionary *dict = noti.userInfo;
// 间隔时间
NSTimeInterval interval = [dict[UIKeyboardAnimationDurationUserInfoKey]doubleValue];
// 键盘的高度
// 停止后的Y值
CGRect keyboardRect = [dict[UIKeyboardFrameEndUserInfoKey]CGRectValue];
CGFloat keyboardEndY = keyboardRect.origin.y;
// 没出现时的Y值
CGRect tempRect = [dict[UIKeyboardFrameBeginUserInfoKey]CGRectValue];
CGFloat keyboardBeginY = tempRect.origin.y;
// 对 tableView 执行动画,向上平移
[UIViewanimateWithDuration:intervalanimations:^{
self.view.transform =CGAffineTransformMakeTranslation(0, (keyboardEndY - keyboardBeginY));
}];
}
#pragma mark -
#pragma mark - 键盘即将隐藏的时候调用
- (void)keyboardWillDisAppear:(NSNotification *)noti {
// 取出通知中的信息
NSDictionary *dict = noti.userInfo;
// 间隔时间
NSTimeInterval interval = [dict[UIKeyboardAnimationDurationUserInfoKey]doubleValue];
[UIViewanimateWithDuration:intervalanimations:^{
// CGAffineTransformIdentity 恢复 transform的设置
self.view.transform =CGAffineTransformIdentity;
}];
}
//——————————————————————取消键盘---收起键盘
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.tf resignFirstResponder];// 或者 [self.view endEditing:YES];
}