1. 程式人生 > >iOS鍵盤事件實現、控制

iOS鍵盤事件實現、控制

記錄於2013/6/26:   一.點選鍵盤上的“Done”按鈕關閉鍵盤 1.檢視控制器實現UITextFieldDelegate協議 2.設定文字框屬性      textField.returnKeyType = UIReturnKeyDone;    textField.delegate=self;   3. 重寫協議方法  - (BOOL)textFieldShouldReturn:(UITextField *)textField {     [textFieldresignFirstResponder]; // the user pressed the "Done" button, so dismiss the keyboard returnYES; }   二.點選鍵盤上的“Next”按鈕實現文字框焦點跳轉 1.檢視控制器實現UITextFieldDelegate協議 2.設定文字框屬性      tx_username.returnKeyType = UIReturnKeyNext;    tx_username.delegate =self;    3.重寫協議方法  - (BOOL)textFieldShouldReturn:(UITextField *)textField {    if([tx_username isFirstResponder]) {         [tx_email becomeFirstResponder];     }elseif([tx_email isFirstResponder]) {         [tx_password becomeFirstResponder];     } elseif([tx_password isFirstResponder]) {         [tx_conPassword becomeFirstResponder];     } elseif([tx_conPassword isFirstResponder]) {         [self performSelector:@selector(userRegister)];//tx_conPassword指定了<span style="font-family: Menlo; font-size: 11px;">UIReturnKeyJoin型別後呼叫的方法</span></span>     }     returnYES; }    
UIScrollView頁面下點選TextField鍵盤事件:  不能改變scrollView的frame,得改變contentOffSet調整。 (1) #pragma mark #pragma mark - Textfield delegate //增加手勢  點選輸入框以外區域降下鍵盤 -(void)RegisterViewAddGest:(UIImageView *)view {     view.userInteractionEnabled = YES;     //單擊事件     UITapGestureRecognizer *taprecognizer;     taprecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(RegisterKeyDown:)];     taprecognizer.numberOfTapsRequired = 1;     [view addGestureRecognizer:taprecognizer];     [taprecognizer release]; } //鍵盤下降的時候 -(void)RegisterKeyDown:(UITapGestureRecognizer *)taprecognizer{     DebugLog(@"點選背景鍵盤下降");     if (taprecognizer.numberOfTapsRequired == 1) {         [self.nameTextField resignFirstResponder];         [self.phoneTextField resignFirstResponder];         [self.addressTextField resignFirstResponder];         [self.goodsTextField resignFirstResponder];         [self.numberTextField resignFirstResponder];         [self.timeTextField resignFirstResponder];         [self.attendTextField resignFirstResponder];                 NSTimeInterval animationDuration = 0.30f;         [UIView beginAnimations:@"ResizeForKeyboard"context:nil];         [UIView setAnimationDuration:animationDuration];                 if (self.buyNowScrollView.contentOffset.y >= self.buyNowScrollView.contentSize.height - self.buyNowScrollView.frame.size.height) {             self.buyNowScrollView.contentOffset = CGPointMake(0, self.buyNowScrollView.contentSize.height - self.buyNowScrollView.frame.size.height);         }         [UIViewcommitAnimations];             } } //輸入框return的時候 -(BOOL)textFieldShouldReturn:(UITextField *)textField {     if ([textField isEqual:self.nameTextField]) {         [self.phoneTextFieldbecomeFirstResponder];     }elseif ([textField isEqual:self.phoneTextField]) {         [self.addressTextFieldbecomeFirstResponder];     }elseif ([textField isEqual:self.addressTextField]) {         [self.goodsTextFieldbecomeFirstResponder];     }elseif ([textField isEqual:self.goodsTextField]) {         [self.numberTextFieldbecomeFirstResponder];     }elseif ([textField isEqual:self.numberTextField]) {         [self.timeTextFieldbecomeFirstResponder];     }elseif ([textField isEqual:self.timeTextField]) {         [self.attendTextFieldbecomeFirstResponder];     }else {         DebugLog(@"輸入框return的時候");         [textField resignFirstResponder];                 NSTimeInterval animationDuration = 0.30f;         [UIViewbeginAnimations:@"ResizeForKeyboard"context:nil];         [UIViewsetAnimationDuration:animationDuration];           self.buyNowScrollView.contentOffset = CGPointMake(0, self.buyNowScrollView.contentSize.height - self.buyNowScrollView.frame.size.height);                 [UIViewcommitAnimations];     }         returnYES; } //輸入框彈出的時候 - (void)textFieldDidBeginEditing:(UITextField *)textField {     DebugLog(@"鍵盤彈出"); CGRect frame = textField.frame; int offset = frame.origin.y + 70 - (self.view.frame.size.height - 216.0);//鍵盤高度216     NSTimeInterval animationDuration = 0.30f; [UIView beginAnimations:@"ResizeForKeyBoard"context:nil]; [UIView setAnimationDuration:animationDuration];     if(offset > 0) {         self.buyNowScrollView.contentOffset = CGPointMake(0, offset + 10); }else{         self.buyNowScrollView.contentOffset = CGPointMake(0, 0);     }     [UIView commitAnimations]; }       (2)改寫(1)的,更加穩定適用了   暫時還是很完美的 裡面的都是textField嵌入在scroll裡面的使用 #pragma mark - UITextFieldDelegate - (BOOL)textFieldShouldReturn:(UITextField *)textField{     //401為普通會員賬號 402為普通會員密碼 403為普通會員密碼確認 404為普通會員暱稱     //411為髮型師會員賬號 412為髮型師會員密碼 413為髮型師會員密碼確認 414為髮型師會員姓名 415為髮型師會員手機     //鍵盤高度216     if(textField.tag <= 404){         if ([textField isEqual:self.commonUserText]) {             [self.commonPassTextbecomeFirstResponder];         }elseif ([textField isEqual:self.commonPassText]){             [self.commonRepeatPassTextbecomeFirstResponder];         }elseif ([textField isEqual:self.commonRepeatPassText]){             [self.commonNickTextbecomeFirstResponder];         }else{             for (UIView *subView inself.commonScrollView.subviews) {                 if ([subView isKindOfClass:[UITextFieldclass]]) {                     [subView resignFirstResponder];                 }             }                         if (self.commonScrollView.contentSize.height - self.commonScrollView.contentOffset.y < self.commonScrollView.frame.size.height) {                 [self.commonScrollViewsetContentOffset:CGPointMake(0, self.commonScrollView.contentSize.height - self.commonScrollView.frame.size.height) animated:YES];             }         }     }else{         if ([textField isEqual:self.barberUserText]) {             [self.barberPassTextbecomeFirstResponder];         }elseif ([textField isEqual:self.barberPassText]){             [self.barberRepeatPassTextbecomeFirstResponder];         }elseif ([textField isEqual:self.barberRepeatPassText]){             [self.barberNickTextbecomeFirstResponder];         }elseif ([textField isEqual:self.barberNickText]){             [self.barberTelphoneTextbecomeFirstResponder];         }else{             for (UIView *subView inself.barberScrollView.subviews) {                 if ([subView isKindOfClass:[UITextFieldclass]]) {                     [subView resignFirstResponder];                 }             }                         //scroll的內容高度 - scroll的contentOffSet高度 < scroll的frane高度             if (self.barberScrollView.contentSize.height - self.barberScrollView.contentOffset.y < self.barberScrollView.frame.size.height) {                 [self.barberScrollViewsetContentOffset:CGPointMake(0, self.barberScrollView.contentSize.height - self.barberScrollView.frame.size.height) animated:YES];             }         }     }     returnYES; } - (void)textFieldDidBeginEditing:(UITextField *)textField{     //401為普通會員賬號 402為普通會員密碼 403為普通會員密碼確認 404為普通會員暱稱     //411為髮型師會員賬號 412為髮型師會員密碼 413為髮型師會員密碼確認 414為髮型師會員姓名 415為髮型師會員手機     //鍵盤高度216     CGRect frame = textField.frame;     int offset;     if(textField.tag <= 404){         //scroll的y座標 + textField在Scroll上的y座標 + textField高度 - 除鍵盤高度外螢幕高度         offset = frame.origin.y + 110 + 30 + 6 - (SCREEN_HEIGHT - 252);         NSTimeInterval animationDuration = 0.30f;         [UIViewbeginAnimations:@"ResizeForKeyBoard"context:nil];         [UIViewsetAnimationDuration:animationDuration];         if(offset > 0)         {             [self.commonScrollViewsetContentOffset:CGPointMake(0, offset) animated:YES];         }else{             [self.commonScrollViewsetContentOffset:CGPointMake(0, 0) animated:YES];         }         [UIViewcommitAnimations];     }else {         NSLog(@"%f",SCREEN_HEIGHT);         offset = frame.origin.y + 146 - (SCREEN_HEIGHT - 252);                 if(offset > 0)          {             [self.barberScrollViewsetContentOffset:CGPointMake(0, frame.origin.y + 146 - (SCREEN_HEIGHT - 252)) animated:YES];         }else{             [self.barberScrollViewsetContentOffset:CGPointMake(0, 0) animated:YES];         }     } } - (void)textFieldDidEndEditing:(UITextField *)textField{         //scroll的內容高度 - scroll的contentOffSet高度 < scroll的frane高度     if (self.commonScrollView.contentSize.height - self.commonScrollView.contentOffset.y < self.commonScrollView.frame.size.height) {         [self.commonScrollViewsetContentOffset:CGPointMake(0, self.commonScrollView.contentSize.height - self.commonScrollView.frame.size.height) animated:YES];     }         if (self.barberScrollView.contentSize.height - self.barberScrollView.contentOffset.y < self.barberScrollView.frame.size.height) {         [self.barberScrollViewsetContentOffset:CGPointMake(0, self.barberScrollView.contentSize.height - self.barberScrollView.frame.size.height) animated:YES];     }     }          
  UIView下的介面移動避免鍵盤遮蓋方法:   - (void)keyboardWillShow:(NSNotification *)noti { //鍵盤輸入的介面調整 //鍵盤的高度 float height = 216.0; CGRect frame = self.view.frame; frame.size = CGSizeMake(frame.size.width, frame.size.height - height); [UIView beginAnimations:@"Curl"context:nil];//動畫開始 [UIView setAnimationDuration:0.30]; [UIView setAnimationDelegate:self]; [self.view setFrame:frame]; [UIView commitAnimations]; }     - (BOOL)textFieldShouldReturn:(UITextField *)textField { // When the user presses return, take focus away from the text field so that the keyboard is dismissed. NSTimeInterval animationDuration = 0.30f; [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; [UIView setAnimationDuration:animationDuration]; CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height); self.view.frame = rect; [UIView commitAnimations]; [textField resignFirstResponder]; return YES; }   - (void)textFieldDidBeginEditing:(UITextField *)textField { CGRect frame = textField.frame; int offset = frame.origin.y + 70 - (self.view.frame.size.height - 216.0); //鍵盤高度216 ,向上提多少在這裡改 NSTimeInterval animationDuration = 0.30f; [UIView beginAnimations:@"ResizeForKeyBoard" context:nil]; [UIView setAnimationDuration:animationDuration]; float width = self.view.frame.size.width; float height = self.view.frame.size.height; if(offset > 0) { CGRect rect = CGRectMake(0.0f, -offset,width,height); self.view.frame = rect; } [UIView commitAnimations]; }