【问题标题】:What is the iPhone's default keyboard animation rate?iPhone 的默认键盘动画速率是多少?
【发布时间】:2010-11-28 00:00:35
【问题描述】:

不久前,我记得在 iPhone 上看到了一个定义键盘动画速率的常量,但我终其一生都无法记住我在哪里看到它......有什么见解吗?

【问题讨论】:

  • 键盘样式和旋转行为在 2.2.1 和 3.0 之间发生了变化;谁说他们不会在以后的版本中改变动画速率?
  • 可以在stackoverflow.com/a/19235995/39946 找到更好的答案。它提供了正确的持续时间和正确的动画曲线。

标签: iphone animation keyboard


【解决方案1】:

UIKeyboardAnimationDurationUserInfoKey 一个 NSValue 对象的键,包含一个以秒为单位标识动画持续时间的双精度值。

【讨论】:

  • 嘿伙计,UIKeyboardAnimationDurationUserInfoKey 是通知的用户信息字典中的关键吗???? -thx
【解决方案2】:
- (NSTimeInterval)keyboardAnimationDurationForNotification:(NSNotification*)notification
{
    NSDictionary* info = [notification userInfo];
    NSValue* value = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval duration = 0;
    [value getValue:&duration];
    return duration;
}

【讨论】:

  • 如果您来这里是为了寻找 MonoTouch 的答案,那就看看e.AnimationDuration
  • 请注意,准确地说,您还应该调整其他动画键,例如UIKeyboardAnimationCurveUserInfoKey
  • @Dan,我是 MonoTouch 用户,但 'e' 是什么?
  • @William:我的意思是你的处理程序中的第二个参数。事件参数。
  • @Dan,您使用的是什么处理程序?我正在使用的处理程序采用单个参数 NSNotification。
【解决方案3】:

由于这是谷歌的第一次点击,我想指出,硬编码 0.3 将意味着当国际用户(例如日本人)在不同大小的键盘之间切换时(当该操作应该即时)。

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

【讨论】:

  • 注意:在撰写本文时(iOS 5.1.1),默认持续时间现在为 0.25 秒。所以就像@greenlight 所说,永远不要硬编码——使用通知的 userInfo 字典中的数据。
【解决方案4】:

为 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;
}

【讨论】:

    【解决方案5】:

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

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

    【讨论】:

    • 单线:double duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    【解决方案6】:

    在 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)
    

    【讨论】:

      【解决方案7】:

      Swift 4 - 为我工作:

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

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

      【讨论】:

        猜你喜欢
        • 2014-09-15
        • 2020-08-05
        • 2014-02-14
        • 2010-12-23
        • 1970-01-01
        • 2012-07-02
        • 1970-01-01
        • 2019-02-03
        • 1970-01-01
        相关资源
        最近更新 更多