我无法在正文视图中使用声明的变量。如果我使用
.fill(colorWheel["darkOrange"])
在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
}
}
答案 0 :(得分:1)
尝试一下。
.fill(self.colorWheel["darkOrange"] ?? Color.green)
如果没有颜色,则将Color.green替换为默认颜色。