What is the iPhone's default keyboard animation rate?
Asked Answered
C

7

36

A while ago I remember seeing a constant of some kind that defined the animation rate of the Keyboard on the iPhone and I can not for the life of me remember where I saw it....any insight?

Chatterjee answered 14/9, 2009 at 0:32 Comment(2)
The keyboard style and rotation behavior was changed between 2.2.1 and 3.0; who's to say they won't change the animation rate in later versions?Colcannon
An even better answer to this can be found at https://mcmap.net/q/267246/-how-to-mimic-keyboard-animation-on-ios-7-to-add-quot-done-quot-button-to-numeric-keyboard It provides the correct duration and correct animtation curve.Pill
F
69
- (NSTimeInterval)keyboardAnimationDurationForNotification:(NSNotification*)notification
{
    NSDictionary* info = [notification userInfo];
    NSValue* value = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval duration = 0;
    [value getValue:&duration];
    return duration;
}
Fitzpatrick answered 17/9, 2009 at 5:58 Comment(5)
If you came here looking for MonoTouch answer, look no further than e.AnimationDuration.Especial
Note that to be precise, you should also adjust for the other animation keys, like UIKeyboardAnimationCurveUserInfoKey.Taker
@Dan, I am a MonoTouch user, but what is 'e'?Ambur
@William: I meant second parameter in your handler. Event args.Especial
@Dan, what handler are you using? The handler I'm using takes a single argument, NSNotification.Ambur
G
16

UIKeyboardAnimationDurationUserInfoKey now is a NSNumber object, that makes the code shorter.

- (void)keyboardWillShowNotification:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];
    NSNumber *number = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    double duration = [number doubleValue];
} 
Gamesome answered 10/1, 2013 at 1:35 Comment(1)
One-liner: double duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];Progressist
V
12

Since this is the first google hit, I'd like to point out that hard-coding 0.3 will mean that your view will incorrectly animate when international users (e.g. Japanese) swap between different-sized keyboards (when that action ought to be instant).

Always use the notification's userInfo dictionary's UIKeyboardAnimationDurationUserInfoKey value - it gets set to 0 when the user is flicking through keyboards.

Velutinous answered 1/3, 2012 at 5:40 Comment(1)
NB: at the time of writing (iOS 5.1.1) the default duration is now 0.25s. So like @Velutinous says, don't ever hard code this - use the data from the notification's userInfo dictionary.Funnel
Y
6

To add a bit more to what Shaggy Frog wrote. The full implementation would be something like:

[[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;
}
Yarvis answered 7/1, 2013 at 4:3 Comment(0)
S
2

UIKeyboardAnimationDurationUserInfoKey The key for an NSValue object containing a double that identifies the duration of the animation in seconds.

Skinner answered 14/9, 2009 at 0:43 Comment(1)
Hey mate, is the key UIKeyboardAnimationDurationUserInfoKey in the user info dictionary of the notification???? -thxChatterjee
M
2

In Swift your code will look like this:

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)
Magnetohydrodynamics answered 9/7, 2016 at 14:8 Comment(0)
C
2

Swift 4 - worked for me:

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

In debug mode my duration was 3.499999

Cenobite answered 20/12, 2017 at 11:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.