简体   繁体   English

如何在 SWIFTUI 主体视图中使用声明的变量

[英]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.

相关问题 如何使用 swiftUI 在视图主体内使用变量? - How to use variable inside the body of the view using swiftUI? 如何从 SwiftUI 视图中更改变量? - How do I change a variable from inside a SwiftUI view? 在 SwiftUI 中实例化自定义视图时,如何动态声明变量类型然后将其用作参数? - How can I dynamically declare a variable type then use it as an argument when instantiating a custom view in SwiftUI? 我可以使用 SwiftUI 工作表视图中的按钮(出现在我的主 SwiftUI 视图顶部)来更改主视图内的子视图吗? - Can I use a button inside a SwiftUI sheet view that appears on top of my main SwiftUI view to change a subview inside the main view? 如何使用可以像List一样填充的主体制作可重复使用的SwiftUI View? - How do I make a reusable SwiftUI View with a body I can fill in like List? 在 SwiftUI 中将视图声明为视图主体内的变量时出现“函数声明不透明的返回类型 [...]”错误 - "Function declares an opaque return type [...]" error when declaring a view as a variable inside the body of a View in SwiftUI 如何在SwiftUI中访问视图的修饰符? - How can I access modifiers of a view in SwiftUI? 如何在不继承 SwiftUI 中其他已定义视图的情况下制作正文,苹果最初是如何做到的? - How can I make body without inheriting from other defined View in SwiftUI, How apple done this at first place? 如何使用 SwiftUI 呈现新视图? - How can I present a new view with SwiftUI? 如何在 function 中传递 SwiftUI 视图? - How can I pass in a SwiftUI View in a function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM