繁体   English   中英

键盘显示时使UITextField动画化

[英]Animating UITextField up when keyboard shows

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

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)
        })
    }
}

我很沮丧 键盘通知会以某种方式影响新动画吗?

您将必须调用这两种方法来上下动画视图。

- (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]);
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM