#import "ViewController.h" #define kScreenSize [UIScreen mainScreen].bounds.size #define kDebugPrint NSLog(@"%s %d",__func__,__LINE__) @interface ViewController () <UITextFieldDelegate> @end @implementation ViewController - (void)dealloc { //销毁的时候 删除观察者 [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [super dealloc]; } /* 键盘在弹出的时候 会给app 发送一个通知 给通知中心 通知中心再把这个通知进行广播。 咱们要想接收到这个广播,那么咱们必需要注册一个观察者对象 当系统通知中心进行广播通知的时候 观察者就能够接收到 注册观察者 必需要在发通知以前 */ /* 目前所学系统单例有哪些 1.NSFileManager 2.UIApplication 3.NSNotificationCenter 4.NSUserDefaults */ - (void)viewDidLoad { [super viewDidLoad]; //1.获取通知中心(单例对象 整个程序只有一个通知中心) NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; //2.注册观察者 /** 第一个参数:任意对象地址 一般写成self 2 : 第一个参数的行为 选择器 3 :通知的名字 4 :谁发的通知 nil 不关心谁发的通知 一旦有对象发送UIKeyboardWillShowNotification 的通知 那么通知中心会让观察者 执行 keyboardWillShow: */ //注册弹出键盘的观察者 [nc addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; //注册 收键盘的观察者 [nc addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil]; [self creatTextFlied]; } #pragma mark - 接收到通知调用 //参数 是 通知对象地址 //接收到弹出键盘通知 self 调用 下面的方法 - (void)keyboardWillShow:(NSNotification *)nf { //获取通知的内容 NSDictionary *dict = nf.userInfo; NSLog(@"dict:%@",dict); //NSNumber *t = dict[@"UIKeyboardAnimationDurationUserInfoKey"]; //获取弹出时间 NSNumber *t = dict[UIKeyboardAnimationDurationUserInfoKey]; UIButton *button1 = (UIButton *)[self.view viewWithTag:201]; UIButton *button2 = (UIButton *)[self.view viewWithTag:202]; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:t.doubleValue]; button1.frame = CGRectMake(0, 200, 100, 50); button2.frame = CGRectMake(kScreenSize.width-100, 200, 100, 50); [UIView commitAnimations]; } - (void)keyboardWillHidden:(NSNotification *)nf { UIButton *button1 = (UIButton *)[self.view viewWithTag:201]; UIButton *button2 = (UIButton *)[self.view viewWithTag:202]; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.25]; button1.frame = CGRectMake(0, 350, 100, 50); button2.frame = CGRectMake(kScreenSize.width-100, 350, 100, 50); [UIView commitAnimations]; } - (void)creatTextFlied { NSArray *arr = @[@"登陆",@"注册"]; for (NSInteger i = 0; i < arr.count; i++) { UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 30+40*i, kScreenSize.width-20, 30)]; textField.borderStyle = UITextBorderStyleLine; textField.tag = 101+i; if (i == 1) { textField.secureTextEntry = YES;//密文 } textField.clearButtonMode = UITextFieldViewModeAlways; [self.view addSubview:textField]; [textField release]; //建立按钮 UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake((kScreenSize.width-100)*i, 350, 100, 50); [button setTitle:arr[i] forState:UIControlStateNormal]; [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; button.tag = 201+i; [self.view addSubview:button]; } UITextField *text1 = (UITextField *)[self.view viewWithTag:101]; UITextField *text2 = (UITextField *)[self.view viewWithTag:102]; //设置代理 text1.delegate = self; text2.delegate = self; } #pragma mark - UITextFieldDelegate协议 //点击return键 键盘通知代理调用 - (BOOL)textFieldShouldReturn:(UITextField *)textField { // //取消第一响应 // [textField resignFirstResponder]; [self hiddenKeyboard]; kDebugPrint; return YES;//通常返回YES return 键是否有效 } - (void)hiddenKeyboard { UITextField *text1 = (UITextField *)[self.view viewWithTag:101]; UITextField *text2 = (UITextField *)[self.view viewWithTag:102]; //取消第一响应 [text1 resignFirstResponder]; [text2 resignFirstResponder]; } //给画布增长 触摸 //点击 屏幕 触摸 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //收键盘 [self hiddenKeyboard]; } - (void)btnClick:(UIButton *)button { kDebugPrint; //收键盘 [self hiddenKeyboard]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end