Group Group Group Group Group Group Group Group Group

How can I slide the Slider under a View in SwiftUI?

There are two views (Slider and Button) under a view (Rectangle):

struct ContentView: View {
    //@State var val = false
    @State var floatVal = 0.0
    
    var body: some View {
        ZStack {
            VStack {
                Text("Control penetration")
                    .font(.title)
                
                //Slider can't slide
                Slider(value: $floatVal, in: 0...100)
                
                //Toggle can't toggle too
                //Toggle(isOn: $val){EmptyView()}
                
                //only button can tap
                Button(action: {
                    print("Tapped!")
                }){
                    Text("Can you tap me???")
                }
                
            }
            .padding()
            
            Rectangle()
                .fill(Color.green.opacity(0.2))
                .allowsHitTesting(false)
        }
    }
}

I use allowsHitTesting() to make the click penetrate to the bottom.

But only the Button can respond to click, the Slider can not slide!

What’s wrong with it? How can I make the Slider respond slide too? thanks a lot!

hi Hopy! I guess the main reason is because you don’t tap a slider. A SO answer says you can’t scroll a scrollview through a top layer either.

Until SwiftUI gets its own version of userInteractionEnabled, you might try creating a UIViewRepresentable version of the rectangle

hi,谢谢您的回答。

其实我是想在Slider上覆盖一个有圆形空洞(用Path eofFill组合1个矩形和1个圆形)的View,然后只有空洞中的Slider可以操作,其它部分的控件不可以响应用户的操作。

如果用userInteractionEnabled,那么所有控件都可以响应用户操作了,这不符合我的要求。

求教,谢谢。