2015-05-14 293 views
0

我已經做到這樣,當用戶點擊文本字段時,視圖會稍微向上移動,以便字段在用戶正在鍵入時仍然可見。它的效果很好,但有時在鍵盤被解散後,而不是返回原來的位置,視圖滑得太遠(在頂部留下一個小的空白黑條)。有誰知道我怎麼才能恢復到原來的位置?這是我的代碼:動畫後返回到原始位置

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 


    _userField.returnKeyType = UIReturnKeyDone; 
    [_userField setDelegate:self]; 

    _passField.returnKeyType = UIReturnKeyDone; 
    [_passField setDelegate:self]; 


    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)]; 
    [self.view addGestureRecognizer:tap]; 
} 


- (void)dismissKeyboard 
{ 
    [_userField resignFirstResponder]; 
    [_passField resignFirstResponder]; 

} 

-(void)textFieldShouldReturn:(UITextField *)textField 
{ 
    [textField resignFirstResponder]; 
} 

-(void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    [self animateTextField:textField up:YES]; 
} 

- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    [self animateTextField:textField up:NO]; 
} 

    -(void)animateTextField:(UITextField*)textField up:(BOOL)up 
    { 
     const int movementDistance = -130; // tweak as needed 
     const float movementDuration = 0.3f; // tweak as needed 

     int movement = (up ? movementDistance : -movementDistance); 

     [UIView beginAnimations: @"animateTextField" context: nil]; 
     [UIView setAnimationBeginsFromCurrentState: YES]; 
     [UIView setAnimationDuration: movementDuration]; 
     self.view.frame = CGRectOffset(self.view.frame, 0, movement); 
     [UIView commitAnimations]; 
    } 
+0

什麼是觸發該方法?你在聽鍵盤通知嗎? – Dare

+0

您未使用AutoLayout? (我不知道,只是想知道)。 對幀的完整理解,確保您之前的位置在加載視圖後存儲(可能會隨着動畫而改變),並且Quartz 2D可以讓您找出奇怪的轉換進入的位置。需要查看其他代碼以便更改後,存儲位置和使用的自動佈局。 –

+0

嗨@StephenJ,是的,我使用AutoLayout。我假設因爲我的代碼將視圖向上移動了一段特定的距離,我只需要在鍵盤解散後將視圖向下移動130。任何想法可能是什麼樣子?請參閱上面的代碼編輯。 – Brittany

回答

0

我建議您使用鍵盤通知來代替。這樣,即使您有多個字段,它也可以工作,您可以使用鍵盤動畫對更改進行動畫製作,並且您將確切知道由於鍵盤顯示而丟失了多少屏幕區域。

首先,在您的視圖控制器,註冊和註銷的通知:

- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil]; 
} 

- (void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:animated]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; 
} 

然後實現當通知被觸發被調用的方法有兩種:

- (void)keyboardWillShowNotification:(NSNotification *)notification { 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]]; 
    [UIView setAnimationCurve:(UIViewAnimationCurve)[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; 
    [self.mainView adjustContentWithKeyboardHeight:keyboardFrame.size.height]; 
    [UIView commitAnimations]; 
} 

- (void)keyboardWillHideNotification:(NSNotification *)notification { 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]]; 
    [UIView setAnimationCurve:(UIViewAnimationCurve)[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [self.mainView adjustContentWithKeyboardHeight:0]; 
    [UIView commitAnimations]; 
} 

最後,實現adjustContentWithKeyboardHeight公開的方法在你看來。由於該方法在UIView動畫中被調用,因此所有更改都將動畫化。

如果您直接移動視圖(將它們全部置於您將移動的容器視圖中),請將原始Y位置保留在私有屬性中,然後從該屬性中減去keyboardHeight減去textField下的剩餘空間,並將其分配給該字段(或根據自己的喜好設置框架):

CGFloat offsetY = keyboardHeight - (CGRectGetHeight(self.bounds) - CGRectGetMaxY(self.containerView.frame)); 
    CGRect containerFrame = self.containerView.frame; 
    containerFrame.origin.y = self.containerViewPositionY - offsetY; 
    self.containerView.frame = containerFrame; 
相關問題