以下是当键盘出现时向上移动的 textField 和 toolBar 的动画。
baseConstraint.constant = 211
self.view.setNeedsUpdateConstraints()
UIView.animateWithDuration(0.30, animations: {
self.view.layoutIfNeeded()
})
它很接近但不完全相同。你会如何修改上面的动画?
编辑:
这是使用下面答案的最终代码!
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()
})
}
最佳答案
可以从keyboardWillShow: notifications上的userInfo字典中获取动画时长和动画曲线。
先注册通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
然后从通知 userInfo 键中获取值。
- (void)keyboardWillShow:(NSNotification*)notification {
NSNumber *duration = [notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [notification.userInfo objectForKey: UIKeyboardAnimationCurveUserInfoKey];
// Do stuff with these values.
}
这些键还有很多,您也可以从 UIKeyboardWillDismiss 通知中获取它们。
此功能一直可用到 iOS 3.0 :D
这是文档:
快速版本
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]
}
关于ios - iOS8出现键盘的动画速度是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24923086/