gpt4 book ai didi

ios - 键盘动画延迟

转载 作者:行者123 更新时间:2023-11-30 12:06:31 25 4
gpt4 key购买 nike

我试图在键盘显示和隐藏时移动 ScrollView 内容。我正在做基本功能。但键盘先可见,向上移动动画有延迟,更新到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是scrollview的底部约束。

我做错了什么?任何帮助,将不胜感激。谢谢!

最佳答案

您尝试给出 ScrollView 的高度并使用主视图高度进行计算 - 键盘高度和键盘更改框架将工作延迟,如果您想捕获键盘打开和隐藏,您应该将它们添加到 viewdidload 上:

全局:var KeyboardHeight:CGFloat?

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)

加载时计算 ScrollView 高度

self.scrollviewHeight.constant = self.view.frame.height - keyboardHeight - self.textView.frame.height

你可以用UIView.animation这样计算

关于ios - 键盘动画延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46618836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com