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

Sho*_*uit 35 iphone keyboard animation

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

Sha*_*rog 68

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

  • 请注意,准确地说,您还应该调整其他动画键,例如"UIKeyboardAnimationCurveUserInfoKey". (3认同)

Zhe*_*ing 16

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

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


小智 11

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

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

  • 注意:在撰写本文时(iOS 5.1.1),默认持续时间现在为0.25秒.就像@greenlight所说的那样,不要硬编码 - 使用通知的userInfo字典中的数据. (3认同)

Ben*_*man 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;
}
Run Code Online (Sandbox Code Playgroud)