手势开始处理逻辑

This commit is contained in:
anlicheng 2025-07-01 00:46:17 +08:00
parent e7549803b6
commit d3c5004797

View File

@ -54,6 +54,15 @@ struct SearchView: View {
.opacity(showSearchResult && searchModel.dramaGroups.isEmpty ? 1 : 0)
}
}
.background(
GestureObserver {
//
if isFocused {
isFocused = false
}
}
.frame(width: 0, height: 0)
)
.navigationTitle("")
.toolbar {
ToolbarItem(placement: .principal) {
@ -123,6 +132,42 @@ struct SearchDramaGroupView: View {
}
}
struct GestureObserver: UIViewControllerRepresentable {
var onGestureBegin: () -> Void
func makeUIViewController(context: Context) -> UIViewController {
let controller = UIViewController()
DispatchQueue.main.async {
if let nav = controller.navigationController,
let gesture = nav.interactivePopGestureRecognizer {
gesture.addTarget(context.coordinator, action: #selector(Coordinator.handleGesture(_:)))
}
}
return controller
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) { }
func makeCoordinator() -> Coordinator {
Coordinator(onGestureBegin: onGestureBegin)
}
class Coordinator: NSObject {
var onGestureBegin: () -> Void
init(onGestureBegin: @escaping () -> Void) {
self.onGestureBegin = onGestureBegin
}
@objc func handleGesture(_ gesture: UIScreenEdgePanGestureRecognizer) {
if gesture.state == .began {
onGestureBegin()
}
}
}
}
//
//#Preview {
// SearchView()