什么是iPhone的默认键盘动画速率?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了什么是iPhone的默认键盘动画速率?相关的知识,希望对你有一定的参考价值。

前一段时间我记得看到一种常量定义了iPhone上键盘的动画速率,我不能为我的生活记住我在哪里看到它......任何见解?

答案
- (NSTimeInterval)keyboardAnimationDurationForNotification:(NSNotification*)notification
{
    NSDictionary* info = [notification userInfo];
    NSValue* value = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval duration = 0;
    [value getValue:&duration];
    return duration;
}
另一答案

UIKeyboardAnimationDurationUserInfoKey现在是一个NSNumber对象,它使代码更短。

- (void)keyboardWillShowNotification:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];
    NSNumber *number = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    double duration = [number doubleValue];
} 
另一答案

由于这是第一个谷歌热门,我想指出,硬编码0.3意味着当国际用户(例如日语)在不同大小的键盘之间切换时(当该动作应该是即时的)时,您的视图将会错误地生成动画。 。

始终使用通知的userInfo字典的UIKeyboardAnimationDurationUserInfoKey值 - 当用户轻击键盘时,它将设置为0。

另一答案

再加上Shaggy Frog所写的内容。完整的实现将是这样的:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardMovement:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardMovement:)
                                             name:UIKeyboardWillHideNotification
                                           object:nil];


-(void)keyboardMovement:(NSNotification *)notification{
    if (_numericKeyboardShowing == false){
        [UIView animateWithDuration:[self keyboardAnimationDurationForNotification:notification] delay:0
                        options:UIViewAnimationCurveEaseInOut
                     animations:^ {
                         self.bottomContainerView.center = CGPointMake(self.bottomContainerView.center.x, (self.bottomContainerView.center.y - 218));
                                  }
                     completion:NULL];

    _numericKeyboardShowing = true;
   }
   else{
    [UIView animateWithDuration:[self keyboardAnimationDurationForNotification:notification] delay:0
                        options:UIViewAnimationCurveLinear
                     animations:^ {
                         self.bottomContainerView.center = CGPointMake(self.bottomContainerView.center.x, (self.bottomContainerView.center.y + 218));
                     }
                     completion:NULL];

    _numericKeyboardShowing = false;
}

- (NSTimeInterval)keyboardAnimationDurationForNotification:(NSNotification *)notification
{
    NSDictionary *info      = [notification userInfo];
    NSValue* value          = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval duration = 0;
    [value getValue:&duration];
    return duration;
}
另一答案

UIKeyboardAnimationDurationUserInfoKey包含double的NSValue对象的键,用于标识动画的持续时间(以秒为单位)。

另一答案

在Swift中,您的代码将如下所示:

let keyboardSize: CGSize = userInfo[UIKeyboardFrameBeginUserInfoKey]!.CGRectValue.size

let animationDuration = ((userInfo[UIKeyboardAnimationDurationUserInfoKey]) as! NSNumber).floatValue
let animationOptions = ((userInfo[UIKeyboardAnimationCurveUserInfoKey]) as! NSNumber).unsignedLongValue

UIView.animateWithDuration(NSTimeInterval(animationDuration), delay: 0,
  options: UIViewAnimationOptions(rawValue: animationOptions),
  animations: { () -> Void in
                self.view.frame.origin.y += keyboardSize.height
                }, 
  completion: nil)
另一答案

Swift 4 - 为我工作:

        if let duration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? Double {
            UIView.animate(withDuration: duration, animations: {
                self.view.layoutIfNeeded()
            })
        }

在调试模式中,我的duration3.499999

以上是关于什么是iPhone的默认键盘动画速率?的主要内容,如果未能解决你的问题,请参考以下文章

iPhone日期选择器而不是键盘?

iphone / Objective c的最佳代码片段网站是啥[重复]

在 iPhone 中,键盘以纵向模式出现,而不是横向模式。有啥解决办法吗?

如何在单击按钮时弹出默认的iphone键盘

iOS 7 - 键盘动画

如何在 iPhone 上默认显示数字键盘?