如何模仿iOS 7上的键盘animation,将“完成”button添加到数字键盘?

我一直在做这样的事情来模仿旧版iOS的键盘animation。

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]; 

但是,这已经停止在iOS7上工作。 看起来像返回的值不再是完全正确的,完成button不再完全模仿键盘显示animation。

在iOS 7中,键盘使用新的未公开的animation曲线。 虽然有些人已经注意到为animation选项使用未公开的值,但我更愿意使用以下内容:

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

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

我有同样的问题,并设法让animation与iOS 7的以下参数工作:

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

编辑:这些值是通过debugging获得,并可以更改与新的iOS版本。 @ DavidBeck的答案适用于我在iOS 7中,所以我在这里链接它。

苹果公司正在使用iOS 7中的值7的未公开的animation。

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

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

您需要基于块的animation的UIViewAnimationOptions被定义为

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

看起来,苹果公司为animation曲线(20 – 16 = 4)保留了4位,允许0到15的值(所以可能有更多的无证数据)。

有了这些知识,您可以简单地将UIViewAnimationCurve转换为UIViewAnimationOptions ,并将其转换为大约16位。 在你的例子中,这意味着:

 options:[[note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue] << 16 

您可以使用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]; 

快乐的编码!

UPDATE

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

如果你的意思是在工具栏中添加button(就像移动Safari一样),你可以使用属性inputAccessoryViewUITextFieldUITextView都有),并省去所有的麻烦。 这个隐藏的gem是鲜为人知的,我很高兴我偶然发现了它。

它适用于iOS 6和iOS 7,我在我的应用程序Routie中使用它。

这对我来说工作得很好……显示键盘时会捕捉持续时间。 我知道硬编码的选项意味着它可以在未来打破,但它适用于7和8。这对我们来说现在已经足够了…

 [UIView animateWithDuration:self.animationDuration delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{ [self.view layoutIfNeeded]; completion:^(BOOL finished) { if (finished) { if (completion) completion(); } }];