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

时间:2009-09-14 00:32:29

标签: iphone animation keyboard

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

7 个答案:

答案 0 :(得分:68)

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

答案 1 :(得分:16)

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

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

答案 2 :(得分:11)

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

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

答案 3 :(得分:6)

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

答案 4 :(得分:2)

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

答案 5 :(得分:2)

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

答案 6 :(得分:1)

斯威夫特4 - 为我工作:

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

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