键盘显示时向上动画UITextField

时间:2014-12-16 03:26:45

标签: ios animation swift

我已经注册了两个观察者,keyboardWillShow:和keyboardWillHide:在NSNotificationCenter中

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)

在这两种方法中,当我尝试为包含UITextView的UIView设置动画时,它会跳转到我想要设置动画的帧,然后动画回到原始位置。我在touchesBegan方法中尝试了相同的动画代码来测试它并在那里工作得很好(动画到我实际设置的新帧而不是跳跃和动画回到原始位置)。

func keyboardWillShow(notification: NSNotification) {
    if let endFrame = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() {
        UIView.animateWithDuration(1.0, animations: { () -> Void in
            self.footerView.center = CGPointMake(self.footerView.center.x, self.footerView.center.y - endFrame.size.height)
        })
    }
}

我很难过。键盘通知是否会以某种方式影响新动画?

1 个答案:

答案 0 :(得分:0)

您必须调用这两种方法来上下调整视图..

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
@try
{
    if (textField == (UITextField *)[self.view viewWithTag:TEXTFIELD_MOBILE_TAG])
    {
        CGRect selfframe = CGRectMake([self.view viewWithTag:VIEW_PARENT_TAG].frame.origin.x, [self.view viewWithTag:VIEW_PARENT_TAG].frame.origin.y - 80, [self.view viewWithTag:VIEW_PARENT_TAG].frame.size.width, [self.view viewWithTag:VIEW_PARENT_TAG].frame.size.height);
        [UIView animateWithDuration:0.3 animations:^{ [self.view viewWithTag:VIEW_PARENT_TAG].frame = selfframe;}];
        [textField becomeFirstResponder];
    }

}
@catch (NSException *exception)
{
    NSLog(@"Error Occured in SignUpViewController :: textViewDidBeginEditing :: %@",[exception description]);
}

}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
@try
{
    if (textField == (UITextField *)[self.view viewWithTag:TEXTFIELD_MOBILE_TAG])
    {
        CGRect selfframe = CGRectMake([self.view viewWithTag:VIEW_PARENT_TAG].frame.origin.x, [self.view viewWithTag:VIEW_PARENT_TAG].frame.origin.y + 80, [self.view viewWithTag:VIEW_PARENT_TAG].frame.size.width, [self.view viewWithTag:VIEW_PARENT_TAG].frame.size.height);
        [UIView animateWithDuration:0.3 animations:^{ [self.view viewWithTag:VIEW_PARENT_TAG].frame = selfframe;}];
        [textField resignFirstResponder];
    }
}
@catch (NSException *exception)
{
    NSLog(@"error Occured in SignUpViewController :: textViewDidEndEditing :: %@",[exception description]);
}

}
相关问题