[英]how can I use a declared variable inside SWIFTUI body view
I cannot use my declared variable inside body view.我不能在 body 视图中使用我声明的变量。 If I use
如果我使用
.fill(colorWheel["darkOrange"])
inside Ellipse, then it breaks, but if I change to .fill(Color.orange)
, which is a system color, then it's fine.在 Ellipse 内部,它会中断,但是如果我更改为
.fill(Color.orange)
,这是一种系统颜色,那么它就可以了。
struct ContentView: View {
//our color palette
var colorWheel : [String: Color] = [
"darkOrange" : Color.init(hex: "F1615D"),
"mediumOrange" : Color.init(hex: "FF8761"),
"darkYellow" : Color.init(hex: "FFC575"),
"lightYellow" : Color.init(hex: "F1FAC6"),
"brightAqua" : Color.init(hex: "79E6E3"),
"lightAqua" : Color.init(hex: "a8e6cf"),
"limeGreen" : Color.init(hex: "dcedc1"),
"brightPeach" : Color.init(hex: "ff8b94"),
"mediumPeach" : Color.init(hex: "ffaaa5"),
"lightPeach" : Color.init(hex: "ffd3b6"),
]
var body: some View {
ZStack{
VStack {
AnimatedContentView()
Spacer()
ZStack {
Ellipse()
.fill(colorWheel["darkOrange"])
.frame(width: 300, height: 150)
Text("Glad you are here.\n Animate this")
.animation(.spring())
.foregroundColor(Color.white)
.font(.system(size: 28))
}
.rotationEffect(.degrees(-24))
}
.foregroundColor(Color.black.opacity(0.7))
.edgesIgnoringSafeArea(.all)
} //end zStack
}
}
Try this.尝试这个。
.fill(self.colorWheel["darkOrange"] ?? Color.green)
Replace Color.green to your default color whatever you want to apply if there is no color.如果没有颜色,则将 Color.green 替换为您想要应用的默认颜色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.