What is the iPhone's default keyboard animation rate?

前端 未结 7 1856
萌比男神i
萌比男神i 2020-12-13 13:21

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.

相关标签:
7条回答
  • 2020-12-13 13:33

    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;
    }
    
    0 讨论(0)
  • 2020-12-13 13:37

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

    0 讨论(0)
  • 2020-12-13 13:39
    - (NSTimeInterval)keyboardAnimationDurationForNotification:(NSNotification*)notification
    {
        NSDictionary* info = [notification userInfo];
        NSValue* value = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
        NSTimeInterval duration = 0;
        [value getValue:&duration];
        return duration;
    }
    
    0 讨论(0)
  • 2020-12-13 13:49

    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

    0 讨论(0)
  • 2020-12-13 13:52

    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.

    0 讨论(0)
  • 2020-12-13 13:54

    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)
    
    0 讨论(0)
提交回复
热议问题