0
我试图在键盘显示和隐藏时移动滚动视图内容。我正在做基本功能。 但键盘首先可见并且向上移动的动画被延迟,并且在更新到ios 11之后它具有更多的延迟。键盘动画延迟
代码:
viewDidLoad(){
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardNotification(notification:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
}
@objc func keyboardNotification(notification: NSNotification) {
if let userInfo = notification.userInfo {
let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
let duration:TimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIViewAnimationOptions.curveEaseInOut.rawValue
let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw)
if (endFrame?.origin.y)! >= UIScreen.main.bounds.size.height {
self.keyboardHeightLayoutConstraint?.constant = 0.0
} else {
self.keyboardHeightLayoutConstraint?.constant = endFrame?.size.height ?? 0.0
}
UIView.animate(withDuration: duration,
delay: TimeInterval(0),
options: animationCurve,
animations: { self.view.layoutIfNeeded() },
completion: nil)
}
}
keyboardHeightLayoutConstraint
是滚动型的底部约束。
我在做什么错了?任何帮助,将不胜感激。谢谢!
使用'UIKeyboardDidChangeFrame'键键盘动画。为了更好的实现,你可以看看这个博客http://eng.moldedbits.com/technical/ios/2017/08/28/typed-notification-ios.html – Aks
@Aks我正在使用UIKeyboardWillChangeFrame。不好吗? –
'WillChange'在框架更改之前发布,请检查此Apple文档以获取关于键盘的不同通知: - https://developer.apple.com/documentation/uikit/uiwindow – Aks