How to mimic Keyboard animation on iOS 7 to add “Done” button to numeric keyboard?

前端 未结 7 995
故里飘歌
故里飘歌 2020-12-02 10:09

I had been doing something like this to mimic the keyboard animation on older version of iOS.

CGRect keyboardBeginFrame;
[[note.userInfo objectForKey:UIKeybo         


        
相关标签:
7条回答
  • 2020-12-02 10:45

    Apple is using some undocumented animation with the value 7 in iOS 7.

    However the declaration of UIViewAnimationCurve defines values for 0 to 3

    typedef enum {
       UIViewAnimationCurveEaseInOut, // 0
       UIViewAnimationCurveEaseIn,
       UIViewAnimationCurveEaseOut,
       UIViewAnimationCurveLinear // 3
    } UIViewAnimationCurve;
    

    The UIViewAnimationOptions you need for block based animations is defined as

    enum {
       // ...
       UIViewAnimationOptionCurveEaseInOut            = 0 << 16,
       UIViewAnimationOptionCurveEaseIn               = 1 << 16,
       UIViewAnimationOptionCurveEaseOut              = 2 << 16,
       UIViewAnimationOptionCurveLinear               = 3 << 16,
    
       UIViewAnimationOptionTransitionNone            = 0 << 20,
       // ...
    };
    

    It seems that Apple reserves 4 bits for the animaton curve (20 - 16 = 4) which allows values from 0 to 15 (so there are probably more undocumented values).

    With this knowledge you can simply transform an UIViewAnimationCurve into UIViewAnimationOptions by shifting it about 16 bits. In your example this means:

    options:[[note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue] << 16
    
    0 讨论(0)
  • 2020-12-02 10:48

    I had the same issue and managed to get the animation working with the following parameters for iOS 7:

        [UIView animateWithDuration:0.5
                              delay:0
             usingSpringWithDamping:500.0f
              initialSpringVelocity:0.0f
                            options:UIViewAnimationOptionCurveLinear
                         animations:animBlock
                         completion:completionBlock];
    

    EDIT: these values were obtained through debug, and can change with new iOS versions. @DavidBeck's answer works for me in iOS 7 also so I'm linking it here.

    0 讨论(0)
  • 2020-12-02 10:49

    If you mean to add the button inside a toolbar (similarly as the mobile Safari does it), you can just use the property inputAccessoryView (both UITextField and UITextView have it) and save yourself all the trouble. This hidden gem is little known, I am glad that I stumbled upon it.

    It works on both iOS 6 and iOS 7, I am using it in my app Routie.

    0 讨论(0)
  • 2020-12-02 10:52

    In iOS 7, the keyboard uses a new, undocumented animation curve. While some have noted that using an undocumented value for the animation option works, I prefer to use the following:

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
    [UIView setAnimationCurve:[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue]];
    [UIView setAnimationBeginsFromCurrentState:YES];
    
    // work
    
    [UIView commitAnimations];
    

    While block based animations are the recommendation, the animation curve returned from the keyboard notification is an UIViewAnimationCurve, while the option you would need to pass to block based animations is an UIViewAnimationOptions. Using the traditional UIView animation methods allows you to pipe the value directly in. Most importantly, this will use the new undocumented animation curve (integer value of 7) and cause the animation to match the keyboard. And, it will work just as well on iOS 6 and 7.

    0 讨论(0)
  • 2020-12-02 10:55

    You can use animateWithDuration block and set curve inside it. It's clean and work well.

    UIViewAnimationCurve curve = [[notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
    double duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    [UIView animateWithDuration:duration
                        delay:0
                      options:UIViewAnimationOptionBeginFromCurrentState 
                   animations:^{
                     [UIView setAnimationCurve:curve];
                     /* ANIMATION HERE */
                     // don't forget layoutIfNeeded if you use autolayout
                   }
                   completion:nil];
    

    Happy coding!

    UPDATE

    You can use a simple UIViewController category written by me https://github.com/Just-/UIViewController-KeyboardAnimation

    0 讨论(0)
  • 2020-12-02 10:57

    This is working just fine for me... The duration is caught when the keyboard is shown. I know hardcoding the options means that it could break in the future, but it works for 7 and 8. That is good enough for us for now...

    [UIView animateWithDuration:self.animationDuration
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         [self.view layoutIfNeeded]; completion:^(BOOL finished) {
                         if (finished)
                         {
                             if (completion)
                                 completion();
                         }
                     }];
    
    0 讨论(0)
提交回复
热议问题