【问题标题】:improve the animation when showing text view on top of keyboard在键盘顶部显示文本视图时改进动画
【发布时间】: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


【解决方案1】:

这是动画曲线。您引用的示例是将其动画曲线及其持续时间与键盘的动画曲线相匹配。你的代码没有。你的结构应该更像这样:

NSNumber* curve = info[UIKeyboardAnimationCurveUserInfoKey]; // *
NSNumber* duration = info[UIKeyboardAnimationDurationUserInfoKey];
[UIView animateWithDuration:duration.floatValue delay:0
                    options:curve.integerValue << 16 // *
                 animations: ^{ // ...

【讨论】:

  • 不管我之前的评论如何,都会完美。谢谢。
  • @Matt,我对键盘动画代码中的动画曲线并不满意。我试过直接使用 UIKeyboardAnimationCurveUserInfoKey 中的曲线值。你怎么知道你需要把它左移 16 位?即使我尝试我的动画与键盘动画不完全匹配。 :(
  • @DuncanC 我查看了文档。
猜你喜欢
  • 1970-01-01
  • 2021-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-27
  • 1970-01-01
  • 2019-04-17
  • 2023-03-09
相关资源
最近更新 更多