Tuesday, February 13, 2024
HomeiOS Developmentios - Methods to block touches particularly by inserting an invisible and...

ios – Methods to block touches particularly by inserting an invisible and intercepting view in SwiftUI?


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 manner as a result of I discover this easier.

Right here is the start line:

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{
                    Colour.crimson.opacity(0.5).onTapGesture {
                        print("TOUCHED RED")
                    }
                    Colour.blue.opacity(0.5).onTapGesture {
                        print("TOUCHED BLUE")
                    }
                }
                if blockTouches {
                    // The view just isn't rendered and touches are usually not blocked right here.
                    Colour.clear.onTapGesture {
                        print("TOUCH BLOCKED!")
                    }
                }
            }
        }
    }
}

Nonetheless, since we have now used Colour.clear, the framework decides it would not have to render that view in any respect, and thus the touches do not get blocked.

I attempted to trick it in varied methods like this:

Rectangle()
    .fill(Colour.clear)
    .onTapGesture {
        print("TOUCH BLOCKED!")
        }
    .background(Colour.clear)

However solely discovered this to work (the slight opacity, that’s):

Colour.white.opacity(0.0001).onTapGesture {
    print("TOUCH BLOCKED!")
}

Is not there a greater/regular option to do it?



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments