Swiftui,如何更改视图';轻触时的阴影半径

Swiftui,如何更改视图';轻触时的阴影半径,swiftui,shadow,Swiftui,Shadow,如何在按下时将阴影半径更改为0,在按完后恢复6这非常简单,只需向阴影添加一个三元运算符,这取决于状态变量 代码示例 Text("xxx") .foregroundColor(.white) .frame(width: .infinity, height: 48) .background(Color.yellow)

如何在按下时将阴影半径更改为0,在按完后恢复6这非常简单,只需向阴影添加一个三元运算符,这取决于状态变量

代码示例

Text("xxx")
                        .foregroundColor(.white)
                        .frame(width: .infinity, height: 48)
                        .background(Color.yellow)
                        .shadow(radius: 6)
                        .onTapGesture(perform: {

                        })

使用自定义的
按钮样式
,您可以在按键开始/结束时做出响应。不幸的是,
ontapposition
本身无法访问新闻的状态:

struct ContentView: View {
    @State var press: Bool = false
    var body: some View {
        Text("xxx")
            .foregroundColor(.white)
            .frame(width: 80, height: 80)
            .background(Color.yellow)
            .shadow(radius: press ? 0 : 6)
            .onTapGesture(perform: {
                withAnimation {
                    press.toggle()
                }
            })
    }
}
`


`

@Turtle这解决了你的问题吗?
struct ContentView : View {
    var body: some View {
        Button(action: {}) {
            Text("xxx")
        }
        .buttonStyle(CustomButtonStyle())
        .frame(height: 48)
        .frame(maxWidth: .infinity)
    }
}


struct CustomButtonStyle : ButtonStyle {
    
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
            .foregroundColor(.white)
            .background(Color.yellow)
            .shadow(radius: configuration.isPressed ? 0 : 6)
    }
}

@State var press: Bool = false
var body: some View {
    Text("xxx")
            .foregroundColor(.white)
            .frame(width: 80, height: 80)
            .background(Color.yellow)
            .shadow(radius: press ? 0 : 6)
            .onTapGesture(perform: {
                withAnimation {
                    press.toggle()
                }
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
                    withAnimation {
                        press.toggle()
                    }
                }
            })
}