#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