iOS8中出现的键盘动画速度是多少?

use*_*622 21 objective-c ios swift ios8

以下是textField和toolBar的动画,当键盘出现时,它会向上移动.

    baseConstraint.constant = 211
    self.view.setNeedsUpdateConstraints()

    UIView.animateWithDuration(0.30, animations: {
        self.view.layoutIfNeeded()
        })
Run Code Online (Sandbox Code Playgroud)

它很接近但不完全相同.你会如何修改上面的动画?

编辑:

以下是使用以下答案的最终代码!

   func keyboardWillShow(aNotification: NSNotification)    {

        let duration = aNotification.userInfo.objectForKey(UIKeyboardAnimationDurationUserInfoKey) as Double
        let curve = aNotification.userInfo.objectForKey(UIKeyboardAnimationCurveUserInfoKey) as UInt

        self.view.setNeedsLayout()
        baseConstraint.constant = 211
        self.view.setNeedsUpdateConstraints()

        UIView.animateWithDuration(duration, delay: 0, options: UIViewAnimationOptions.fromMask(curve), animations: {
        self.view.layoutIfNeeded()
        }, completion: {
        (value: Bool) in println()
        })
}
Run Code Online (Sandbox Code Playgroud)

Joe*_*ell 28

您可以从keyboardWillShow:notifications上的userInfo字典中获取动画持续时间和动画曲线.

首先注册通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
Run Code Online (Sandbox Code Playgroud)

然后从通知userInfo键获取值.

- (void)keyboardWillShow:(NSNotification*)notification {
    NSNumber *duration = [notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [notification.userInfo objectForKey: UIKeyboardAnimationCurveUserInfoKey];

   // Do stuff with these values.
}
Run Code Online (Sandbox Code Playgroud)

这些密钥中有很多,您也可以从UIKeyboardWillDismiss通知中获取它们.

此功能一直可用于iOS 3.0:D

继承人的文件:

https://developer.apple.com/library/ios/documentation/uikit/reference/UIWindow_Class/UIWindowClassReference/UIWindowClassReference.html#//apple_ref/doc/constant_group/Keyboard_Notification_User_Info_Keys

如果您需要帮助使它工作,请告诉我.

Swift版本:

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)

@objc func keyboardWillShow(_ notification: Notification) {
    let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey]
    let curve = notification.userInfo?[UIResponder.keyboardAnimationCurveUserInfoKey]
}
Run Code Online (Sandbox Code Playgroud)


Dam*_*Dam 18

可变持续时间的答案是正确的,并且工作iOS 3到8,但是使用新版本的Swift,答案的代码不再起作用了.也许这是我的错误,但我必须写:

let duration = aNotification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as Double
let curve = aNotification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as UInt

self.view.setNeedsLayout()
//baseConstraint.constant = 211
self.view.setNeedsUpdateConstraints()

UIView.animateWithDuration(duration, delay: 0.0, options: UIViewAnimationOptions(curve), animations: { _ in
    //self.view.layoutIfNeeded()
}, completion: { aaa in
    //(value: Bool) in println()
})
Run Code Online (Sandbox Code Playgroud)

看起来objectForKey不再起作用,转换更严格.


hoj*_*jin 10

swift3

    let duration = noti.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber
    let curve = noti.userInfo?[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber

    self.view.setNeedsLayout()

    UIView.animate(withDuration: TimeInterval(duration), delay: 0, options: [UIViewAnimationOptions(rawValue: UInt(curve))], animations: {
      self.view.layoutIfNeeded()
    }, completion: nil)
Run Code Online (Sandbox Code Playgroud)


lea*_*vez 6

首先,选择的答案是正确的方法。

这里可以提供更多的是动画的真正含义。如果将 UIViewAnimation 块中的所有 CAAnimations 打印出来,将动画曲线设置为键盘通知中提供的动画曲线时,您会发现它是一个 CASpringAnimation。持续时间为0.5,其他参数为:

let ani = CASpringAnimation(keyPath: someKey)
ani.damping = 500
ani.stiffness = 1000
ani.mass = 3
ani.duration = 0.5
Run Code Online (Sandbox Code Playgroud)

上面的代码可以精确地再现动画。

一旦动画曲线设置为键盘一,UIView 动画将忽略参数中的持续时间。(如果您确实要更改持续时间,请调整该mass值。)

  • 您是否从现有动画中查询过? (2认同)

Ska*_*aal 6

Swift 4更新,iOS 11+

首先在视图的生命周期方法中注册通知:

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillShow(notification:)), name: .UIKeyboardWillShow, object: nil)
}
Run Code Online (Sandbox Code Playgroud)

然后在keyBoardWillShow方法中:

@objc func keyBoardWillShow(notification: NSNotification) {
    guard let duration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? Double else {return}
    print(duration) // you got animation's duration safely unwraped as a double
}
Run Code Online (Sandbox Code Playgroud)

最后,不要忘记在deinit方法中删除观察者:

deinit {
    NotificationCenter.default.removeObserver(self)
}
Run Code Online (Sandbox Code Playgroud)