[UIView animateWithDuration:0.6 delay:0.0 options:UIViewAnimationCurveLinear animations:^{
[self.searchField becomeFirstResponder];
} completion:nil];
动画键盘输入,让它看起来好像很远,在屏幕的左上角,以略微弯曲的线向内摆动到它的最终静止点。 无论我使用什么 UIView 动画方法,都会发生这种情况。
更奇怪的是,它只会发生一次。不仅仅是每个 View Controller 一次,而是一次。 Dispatch_once 风格。
我正在寻找的效果(它在我的方法被调用的所有后续时间都有效,并且适用于 [self.view endEditing]
)每次都只是一个标准的线性条目。我需要将它放在一个动画 block 中,以使其与我从状态栏下方滑下 searchField 的动画保持在同一时间安排。
我的问题 - 如何保证 UIView 动画 block 内 [viewObject becomeFirstResponder]
的动画一致?
最佳答案
第一次设置动画时键盘位于左上角的原因是因为它的初始位置和大小为 (0,0,0,0)。因此,在第一次创建(第一个动画 block )时,它从 (0,0,0,0) 动画到屏幕上可见的任何帧。对该动画的后续调用将在正确的初始位置显示键盘,然后正确设置动画。
在我的研究中,我发现有人说将 Apple 内置动画放在动画 block 中是非常糟糕的做法,我同意。
解决这个问题的方法是监听键盘 Action ,从苹果定义的键盘动画中获取属性,并使用相同的属性制作动画。来自 this answer 的类似内容:
设置你的键盘监听:
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)dealloc {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
然后设置函数来模仿它们的键盘动画属性:
- (void)keyboardWillHide:(NSNotification *)n
{
NSDictionary* userInfo = [n userInfo];
// get the size of the keyboard
CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
// resize the scrollview
CGRect viewFrame = self.scrollView.frame;
// I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView.
viewFrame.size.height += (keyboardSize.height - kTabBarHeight);
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:animationDuration];
//Your animations go here
[UIView commitAnimations];
keyboardIsShown = NO;
}
您应该能够从这些代码中提取您需要的内容,并使您的应用看起来完全符合您的期望。祝你好运。
关于ios - 动画 becomeFirstResponder 会导致键盘以摆动效果出现,但只有一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21295946/