feat: 键盘提前加载
This commit is contained in:
parent
6bc0648fc7
commit
8bf24a3afe
@ -20,12 +20,35 @@ struct UserInfo: View {
|
||||
@State private var isKeyboardVisible = false
|
||||
@FocusState private var isTextFieldFocused: Bool
|
||||
|
||||
// 使用静态属性来确保只初始化一次
|
||||
private static let keyboardPreloader: Void = {
|
||||
let textField = UITextField()
|
||||
textField.autocorrectionType = .no
|
||||
textField.autocapitalizationType = .none
|
||||
textField.spellCheckingType = .no
|
||||
textField.isHidden = true
|
||||
|
||||
if let window = UIApplication.shared.windows.first {
|
||||
window.addSubview(textField)
|
||||
textField.becomeFirstResponder()
|
||||
textField.resignFirstResponder()
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
|
||||
textField.removeFromSuperview()
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
private let keyboardPublisher = NotificationCenter.default.publisher(for: UIResponder.keyboardWillShowNotification)
|
||||
.map { _ in true }
|
||||
.merge(with: NotificationCenter.default.publisher(for: UIResponder.keyboardWillHideNotification)
|
||||
.map { _ in false })
|
||||
.receive(on: RunLoop.main)
|
||||
|
||||
init() {
|
||||
// 在初始化时预加载键盘
|
||||
_ = UserInfo.keyboardPreloader
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
ZStack {
|
||||
// 背景色
|
||||
@ -229,6 +252,7 @@ struct UserInfo: View {
|
||||
NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: .main) { _ in
|
||||
withAnimation(.easeInOut(duration: 0.3)) {
|
||||
isKeyboardVisible = false
|
||||
// 当键盘隐藏时,确保预加载的TextField失去焦点
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user