swiftui - Swiftui,点击时如何更改视图的阴影半径
问题描述
Text("xxx")
.foregroundColor(.white)
.frame(width: .infinity, height: 48)
.background(Color.yellow)
.shadow(radius: 6)
.onTapGesture(perform: {
})
当我按下时如何将阴影半径更改为 0 并在完成按下时恢复 6
解决方案
它非常简单,只需在 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()
}
})
}
}
推荐阅读
- git - 如何从 vscode 中删除同步的远程存储库
- slick-3.0 - 我如何根据条件执行或不执行一行 for-comprehension,使用 scala slickdb
- javascript - 在循环中更改 Rect html Canvas 的颜色
- javascript - 如何将 Google Drive API 的 data-src 分配给 HTML 中的 Canvas
- bash - 使用 bash 脚本插入带有 $s 的 SQL 表
- python - Vscode,python 分段错误(核心转储)
- javascript - 如何在 Observable 订阅者角度 8 中返回承诺
- sql - 试图将行转换为列
- javascript - 使用 vuelidate 进行验证
- python - 如何在 Python 中隐藏控制台中的输入?