UIKeyboardWillChangeFrameNotification UIViewAnimationCurve在iOS 7上设置为7

问题描述 投票:6回答:3

我想确定键盘将如何设置动画。在iOS 6上,我获得了UIKeyboardAnimationCurveUserInfoKey的有效值(应该是一个值为0-3的UIViewAnimationCurve),但该函数返回值7.键盘如何动画?可以用7的值做什么?

NSConcreteNotification 0xc472900 {name = UIKeyboardWillChangeFrameNotification; userInfo = {
    UIKeyboardAnimationCurveUserInfoKey = 7;
    UIKeyboardAnimationDurationUserInfoKey = "0.25";
    UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";
    UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 588}";
    UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 372}";
    UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 480}, {320, 216}}";
    UIKeyboardFrameChangedByUserInteraction = 0;
    UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 264}, {320, 216}}";
}}
ios uikeyboard
3个回答
21
投票

键盘似乎使用的是未记录/未知的动画曲线。

但你仍然可以使用它。要将其转换为UIViewAnimationOptions以进行块动画,请将其移位16位

UIViewAnimationCurve keyboardTransitionAnimationCurve;
[[notification.userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey]
                           getValue:&keyboardTransitionAnimationCurve];

keyboardTransitionAnimationCurve |= keyboardTransitionAnimationCurve<<16;

[UIView animateWithDuration:0.5
                  delay:0.0
                options:keyboardTransitionAnimationCurve
             animations:^{
                // ... do stuff here
           } completion:NULL];

或者只是将其作为动画曲线传递。

UIViewAnimationCurve keyboardTransitionAnimationCurve;
[[notification.userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey]
                           getValue:&keyboardTransitionAnimationCurve];

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:keyboardTransitionAnimationCurve];
// ... do stuff here
[UIView commitAnimations];

1
投票

不幸的是我无法发表评论,否则我不会输入新答案。

您还可以使用:

animationOptions | = animationCurve << 16;

这可能是首选,因为它将在animationOptions上保留先前的OR =操作。


0
投票

在Swift 4中

func keyboardWillShow(_ notification: Notification!) {
    if let info = notification.userInfo {
        let keyboardSize = info[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect
        let duration = info[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double 
        let curveVal = (info[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber)?.intValue ?? 7 // default value for keyboard animation
        let options = UIView.AnimationOptions(rawValue: UInt(curveVal << 16))
        UIView.animate(withDuration: duration, delay: 0, options: options, animations: {
        // any operation to be performed
    }, completion: nil)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.