如何在iOS 7上模仿键盘动画,将"完成"按钮添加到数字键盘?

Mat*_*ley 27 keyboard animation objective-c uiview ios

我一直在做这样的事情来模仿旧版iOS上的键盘动画.

CGRect keyboardBeginFrame;
[[note.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardBeginFrame];
self.doneKeyboardButton.frame = CGRectMake(0, (keyboardBeginFrame.origin.y + keyboardBeginFrame.size.height) - 53, 106, 53);
[[[[UIApplication sharedApplication] windows] lastObject] addSubview:self.doneKeyboardButton];

CGPoint newCenter = CGPointMake(self.doneKeyboardButton.superview.frame.origin.x + self.doneKeyboardButton.frame.size.width/2,
                                self.doneKeyboardButton.superview.frame.size.height - self.doneKeyboardButton.frame.size.height/2);

[UIView animateWithDuration:[[note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]-.02
                      delay:.0
                    options:[[note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]
                 animations:^{
                     self.contentView.frame = CGRectOffset(self.contentView.frame, 0, -TextFieldViewMovement);
                     self.doneKeyboardButton.center = newCenter;
                 }
                 completion:nil];
Run Code Online (Sandbox Code Playgroud)

但是,这已停止在iOS7上运行.看起来返回的值不再完全正确,完成按钮不再完全模仿键盘显示动画.

Dav*_*eck 84

在iOS 7中,键盘使用新的未记录的动画曲线.虽然有些人注意到使用动态选项的未记录值有效,但我更喜欢使用以下内容:

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

// work

[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)

虽然建议使用基于块的动画,但是从键盘通知返回的动画曲线是UIViewAnimationCurve,而您需要传递给基于块的动画的选项是UIViewAnimationOptions.使用传统的UIView动画方法可以直接传递值.最重要的是,这将使用新的未记录的动画曲线(整数值为7)并使动画与键盘匹配.而且,它在iOS 6和7上也能正常工作.


ZeC*_*dea 23

我遇到了同样的问题,并设法让动画使用iOS 7的以下参数:

    [UIView animateWithDuration:0.5
                          delay:0
         usingSpringWithDamping:500.0f
          initialSpringVelocity:0.0f
                        options:UIViewAnimationOptionCurveLinear
                     animations:animBlock
                     completion:completionBlock];
Run Code Online (Sandbox Code Playgroud)

编辑:这些值是通过调试获得的,并且可以随新的iOS版本而改变.@ DavidBeck的答案对我来说也适用于iOS 7,所以我在这里链接它.


der*_*imn 10

Apple正在iOS 7中使用一些未记录的动画,其值为7.

但是声明UIViewAnimationCurve定义0到3的值

typedef enum {
   UIViewAnimationCurveEaseInOut, // 0
   UIViewAnimationCurveEaseIn,
   UIViewAnimationCurveEaseOut,
   UIViewAnimationCurveLinear // 3
} UIViewAnimationCurve;
Run Code Online (Sandbox Code Playgroud)

UIViewAnimationOptions你需要的基于块的动画被定义为

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

   UIViewAnimationOptionTransitionNone            = 0 << 20,
   // ...
};
Run Code Online (Sandbox Code Playgroud)

似乎Apple为动画曲线保留4位(20 - 16 = 4),允许从0到15的值(因此可能有更多未记录的值).

有了这些知识,您可以通过将其移动大约16位来简单地转换UIViewAnimationCurveUIViewAnimationOptions.在您的示例中,这意味着:

options:[[note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue] << 16
Run Code Online (Sandbox Code Playgroud)


Ant*_*nko 7

您可以在其中使用animateWithDuration块和设置曲线.它干净而且运作良好.

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];
Run Code Online (Sandbox Code Playgroud)

快乐的编码!

UPDATE

您可以使用我编写的简单UIViewController类别https://github.com/Just-/UIViewController-KeyboardAnimation