In SwiftUI I want to conditionally block touches by inserting a clear view within the hierarchy and intercepting them.
I do not need to do it in a extra summary means as a result of I discover this less complicated.
Right here is the place to begin:
struct ContentView: View {
@State var blockTouches:Bool = false
var physique: some View {
VStack{
Textual content("toggle contact blocking")
.padding()
.padding()
.onTapGesture {
blockTouches.toggle()
}
ZStack{
VStack{
Shade.purple.opacity(0.5).onTapGesture {
print("TOUCHED RED")
}
Shade.blue.opacity(0.5).onTapGesture {
print("TOUCHED BLUE")
}
}
if blockTouches {
// The view isn't rendered and touches are usually not blocked right here.
Shade.clear.onTapGesture {
print("TOUCH BLOCKED!")
}
}
}
}
}
}
Nevertheless, since we’ve got used Shade.clear, the framework decides it would not must render that view in any respect, and thus the touches do not get blocked.
I attempted to trick it in numerous methods like this:
Rectangle()
.fill(Shade.clear)
.onTapGesture {
print("TOUCH BLOCKED!")
}
.background(Shade.clear)
However solely discovered this to work (the slight opacity, that’s):
Shade.white.opacity(0.0001).onTapGesture {
print("TOUCH BLOCKED!")
}
Is not there a greater/regular option to do it?