2015-05-14 249 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; 
相关问题