ios*_*eak 13 iphone objective-c uitextfield becomefirstresponder resignfirstresponder
我目前在键盘上有一个UITextField.点击它时,它应该粘在键盘顶部并平稳向上移动.我不知道键盘的确切持续时间和动画类型,所以它真的很颠簸.这就是我所拥有的:
[theTextView resignFirstResponder];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.25];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
// Frame changes go here (move down 216px)
[UIView commitAnimations];
[theTextView becomeFirstResponder];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.25];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
// Frame changes go here (move up 216px)
[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)
如果有人之前做过这样的事情,我想知道你用来使动画变得平滑的设置,并让它看起来像是"卡在"键盘的顶部.
rob*_*off 45
UIKit UIKeyboardWillShowNotification在显示键盘时以及UIKeyboardWillHideNotification隐藏键盘时发布.这些通知包含正确设置动画的所需信息UITextField.
假设您的UITextField名字叫做"被叫" myTextField.
首先,您需要在某处注册通知.您注册的位置取决于移动的对象myTextField.在我的项目中,该字段的superview负责,因为我从一个笔尖加载我的UI,我在superview中执行awakeFromNib:
- (void)awakeFromNib
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideOrShow:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideOrShow:) name:UIKeyboardWillShowNotification object:nil];
}
Run Code Online (Sandbox Code Playgroud)
如果你用a UIViewController来移动字段,你可能想要进行viewWillAppear:animated:.
您应该在您的dealloc或中取消注册viewWillDisappear:animated::
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Run Code Online (Sandbox Code Playgroud)
当然,这个keyboardWillHideOrShow:方法很棘手.首先,我从通知中提取动画参数:
- (void)keyboardWillHideOrShow:(NSNotification *)note
{
NSDictionary *userInfo = note.userInfo;
NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
Run Code Online (Sandbox Code Playgroud)
该keyboardFrame是全局坐标系统相同.我需要的帧转换到同一坐标系myTextField.frame,并且myTextField.frame是坐标系中myTextField.superview:
CGRect keyboardFrameForTextField = [self.myTextField.superview convertRect:keyboardFrame fromView:nil];
Run Code Online (Sandbox Code Playgroud)
接下来,我计算要myTextField移动到的帧.新框架的下边缘应该等于键盘框架的上边缘:
CGRect newTextFieldFrame = self.myTextField.frame;
newTextFieldFrame.origin.y = keyboardFrameForTextField.origin.y - newTextFieldFrame.size.height;
Run Code Online (Sandbox Code Playgroud)
最后,我myTextField使用键盘使用的相同动画参数设置其新帧的动画:
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{
self.myTextField.frame = newTextFieldFrame;
} completion:nil];
}
Run Code Online (Sandbox Code Playgroud)
这里全部放在一起:
- (void)keyboardWillHideOrShow:(NSNotification *)note
{
NSDictionary *userInfo = note.userInfo;
NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect keyboardFrameForTextField = [self.myTextField.superview convertRect:keyboardFrame fromView:nil];
CGRect newTextFieldFrame = self.myTextField.frame;
newTextFieldFrame.origin.y = keyboardFrameForTextField.origin.y - newTextFieldFrame.size.height;
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{
self.myTextField.frame = newTextFieldFrame;
} completion:nil];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7672 次 |
| 最近记录: |