首页 > 解决方案 > Swiftui,点击时如何更改视图的阴影半径

问题描述

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

                        })

当我按下时如何将阴影半径更改为 0 并在完成按下时恢复 6

标签: swiftuishadow

解决方案


它非常简单,只需在 shadow 中添加一个三元运算符,这取决于状态 var

代码示例

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()
                }
            })
    }
}

推荐阅读