I have a very simple app which only loads a webview. That url should be changeable through a hidden gesture. Now I struggle with passing the tap from the gesture recognizer to the webview. Am I misunderstanding a concept here?
struct ContentView: View {
@State private var url: URL? = UserDefaults.standard.url(forKey: "savedURL")
@State private var showAlert = false
@State private var urlString = ""
var body: some View {
ZStack {
if let url = url {
WebView(url: $url)
.edgesIgnoringSafeArea(.all)
} else {
Text("No URL provided")
.foregroundColor(.gray)
}
TripleThreeFingerTapGesture {
showAlert = true
}
.frame(width: 0, height: 0)
}
.alert("Enter URL", isPresented: $showAlert) {
TextField("URL", text: $urlString)
Button("OK") {
if let newURL = URL(string: urlString) {
url = newURL
UserDefaults.standard.set(newURL, forKey: "savedURL")
}
}
Button("Cancel", role: .cancel) { }
}
}
}
var action: (() -> Void)?
init(action: @escaping () -> Void) {
super.init(target: nil, action: nil)
self.action = action
self.numberOfTapsRequired = 3
self.numberOfTouchesRequired = 3
self.addTarget(self, action: #selector(handleTap))
}
@objc private func handleTap() {
action?()
}
}
struct TripleThreeFingerTapGesture: UIViewRepresentable {
var action: () -> Void
func makeUIView(context: Context) -> UIView {
let view = UIView(frame: .zero)
let recognizer = TripleThreeFingerTapRecognizer(action: action)
view.addGestureRecognizer(recognizer)
return view
}
func updateUIView(_ uiView: UIView, context: Context) {}
}