【发布时间】:2014-02-14 21:46:25
【问题描述】:
当UIKeyboardWillShowNotification 像下面这样广播时,我正在制作动画以在顶部键盘上显示文本视图
-(void) keyboardWillShow:(NSNotification *)notification{
// get keyboard size and loctaion
NSDictionary *info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
NSNumber *duration = [notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
__block CGRect containerFrame = containerView.frame;
containerFrame.origin.y = self.view.bounds.size.height - (kbSize.height + containerFrame.size.height);
[UIView animateWithDuration:[duration doubleValue]
delay:0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
containerView.frame = containerFrame;
} completion:^(BOOL finished) {
nil;
}
];
}
动画不够好。如果您可以尝试一下,并且在动画结束时会看到小问题。
我搜索了一下,发现还有另一种制作动画的方法,而且效果很好:
-(void) keyboardWillShow:(NSNotification *)note{
// get keyboard size and loctaion
CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
// Need to translate the bounds to account for rotation.
keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];
// get a rect for the textView frame
CGRect containerFrame = containerView.frame;
containerFrame.origin.y = self.view.bounds.size.height - (keyboardBounds.size.height + containerFrame.size.height);
// animations settings
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:[duration doubleValue]];
[UIView setAnimationCurve:[curve intValue]];
// set views with new info
containerView.frame = containerFrame;
// commit animations
[UIView commitAnimations];
}
但是,根据文档,我们不建议在 iOS 4 及更高版本中使用此动画...
与后一种代码相比,我无法区分我的代码。为什么我的代码不能做完美的动画。如果您有任何想法,请提供帮助。
【问题讨论】:
-
你能详细说明一下“动画结尾的小问题”是什么意思吗?
-
除非我弄错了,否则在 iOS 7 中做这种事情的最好方法是使用 Autolayout。我很确定您可以设置一个约束,当键盘不存在时,视图将保持与屏幕底部的距离,或在键盘存在时保持与键盘顶部的距离。
-
@JohnEstropia:试试看,你会看到的。
标签: ios objective-c uitextview uiviewanimation uikeyboard