我有一个文本字段,我可以使用键盘将其移动。但是,动画不同步。文本框架在键盘持续时间之前向上移动约0.5秒。我认为他们应该同步,但我无法弄清楚问题。下面是我的键盘功能动画。
通知观察员:
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardWillShow), name: .UIKeyboardWillShow, object: nil)
动画功能:
@objc func handleKeyboardWillShow(notification: NSNotification){
if let keyboardFrame: NSValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue, let keyboardDuration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? Double, let tabBarHeight = tabBarController?.tabBar.frame.size.height{
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
UIView.animate(withDuration: keyboardDuration, animations: {
self.textfieldBottomeConstraint?.constant = -keyboardHeight + tabBarHeight
self.view.layoutIfNeeded()
})
}
}
我确实试过了,但仍然得到了相同的结果:
self.textfieldBottomeConstraint?.constant = -keyboardHeight + tabBarHeight
UIView.animate(withDuration: keyboardDuration, animations: {
self.view.layoutIfNeeded()
})
答案 0 :(得分:0)
要尝试两件事:
尝试使用UIKeyboardWillChangeFrame
代替UIKeyboardWillShow
如果它没有改变任何内容,请尝试在动画中使用键盘曲线。
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationCurve(context.animationCurve)
UIView.setAnimationDuration(context.animationDuration)
曲线和持续时间
public var animationCurve: UIViewAnimationCurve {
let value = userInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber
return UIViewAnimationCurve(rawValue: value.intValue)!
}
public var animationDuration: Double {
return userInfo[UIKeyboardAnimationDurationUserInfoKey] as! Double
}
答案 1 :(得分:0)
键盘通知还包括一个
UIKeyboardAnimationCurveUserInfoKey
和UIKeyboardAnimationDurationUserInfoKey
。您可以查询这些值以确保动画使用与键盘相同的持续时间和动画曲线。 (在Xcode帮助系统中搜索“键盘通知用户信息键”,以查找键盘通知中提供的所有不同键/值对。