【发布时间】:2018-03-30 21:36:58
【问题描述】:
我有一个文本字段,我可以使用键盘将其向上移动。但是,动画不同步。文本字段框架比键盘持续时间提前约 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()
})
【问题讨论】:
-
您还需要保持曲线同步。见
UIKeyboardAnimationCurveUserInfoKey -
我认为是因为您将 tabBarHeight 添加到 height ,所以动画持续时间不正确
-
我还删除了标签栏高度进行测试,但仍然得到相同的结果
标签: swift uiview uiviewanimation nsnotificationcenter