feat: 键盘提前加载

This commit is contained in:
jinyaqiu 2025-09-01 14:10:44 +08:00
parent 6bc0648fc7
commit 8bf24a3afe

View File

@ -20,12 +20,35 @@ struct UserInfo: View {
@State private var isKeyboardVisible = false @State private var isKeyboardVisible = false
@FocusState private var isTextFieldFocused: Bool @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) private let keyboardPublisher = NotificationCenter.default.publisher(for: UIResponder.keyboardWillShowNotification)
.map { _ in true } .map { _ in true }
.merge(with: NotificationCenter.default.publisher(for: UIResponder.keyboardWillHideNotification) .merge(with: NotificationCenter.default.publisher(for: UIResponder.keyboardWillHideNotification)
.map { _ in false }) .map { _ in false })
.receive(on: RunLoop.main) .receive(on: RunLoop.main)
init() {
//
_ = UserInfo.keyboardPreloader
}
var body: some View { var body: some View {
ZStack { ZStack {
// //
@ -229,6 +252,7 @@ struct UserInfo: View {
NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: .main) { _ in NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: .main) { _ in
withAnimation(.easeInOut(duration: 0.3)) { withAnimation(.easeInOut(duration: 0.3)) {
isKeyboardVisible = false isKeyboardVisible = false
// TextField
} }
} }
} }