EV3*_*EST 4 view modifier conditional-statements swift swiftui
我的代码如下:
Rectangle()
.fill(Color.red)
.frame(width: 60, height: 60, alignment: .center)
.cornerRadius(recording ? 5 : 30)
Run Code Online (Sandbox Code Playgroud)
所以我想知道是否.frame可以像现在这样有条件.cornerRadius。我这样做是为了改变形状,但是我还需要在它变形时使其变小。语音备忘录应用程序的录音按钮就是一个例子。
jn_*_*pdx 10
如果您正在谈论完全不使用帧修改器(或提供一种干净的方法来执行不同的帧),那么ViewModifier可能是一个不错的选择:
struct ContentView: View {
@State private var recording = false
var body: some View {
Rectangle()
.fill(Color.red)
.modifier(CustomFrameModifier(active: recording))
.cornerRadius(recording ? 5 : 30)
}
}
struct CustomFrameModifier : ViewModifier {
var active : Bool
@ViewBuilder func body(content: Content) -> some View {
if active {
content.frame(width: 60, height: 60, alignment: .center)
} else {
content
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2748 次 |
| 最近记录: |