简体   繁体   English

Swiftui:从 JSON 获取动态颜色渐变?

[英]Swiftui: Fetch Dynamic Color Gradient from JSON?

For every item in an array, I want to load a dynamic gradient of colors that are defined in the JSON.对于数组中的每个项目,我想加载 JSON 中定义的 colors 的动态梯度。 There are different colors in each item.每一项都有不同的 colors。

In the JSON, I'm storing hex values in [String].在 JSON 中,我将十六进制值存储在 [String] 中。 I can use these to load Color(hex: String) using the extension from here: Use Hex color in SwiftUI我可以使用这些从这里的扩展加载颜色(十六进制:字符串): 在 SwiftUI 中使用十六进制颜色

I was thinking along the following lines for implementation, but Xcode throws all sorts of errors and besides, it feels like a hack.我正在考虑按照以下方式实现,但是 Xcode 会引发各种错误,此外,感觉就像是 hack。 What am I missing?我错过了什么?

Example model:示例 model:

struct Item: Codable, Identifiable, Hashable {
    let colors: [String]
}

Example Detailview:示例详细视图:

struct DetailView: View {

    let item: Item
    let gradientColors: [Color]

    init(item: Item){
        self.item = item
        ForEach(item.colors) { color in
            gradientColors.append(Color(hex: color))
        }
    }

    var body: some View {
        Circle()
            .background(LinearGradient(gradient: Gradient(colors: gradientColors), startPoint: .leading, endPoint: .trailing))
    }
}

In SwiftUI ForEach is a view that lets you pass a collection of data views similar to the subview of UIView in UIKit.在 SwiftUI 中, ForEach是一个视图,可让您传递数据视图的集合,类似于UIView中的 UIView 的子视图。 so you can not use it inside the init method.所以你不能在 init 方法中使用它。 For more ( https://developer.apple.com/documentation/swiftui/foreach )更多信息( https://developer.apple.com/documentation/swiftui/foreach

The correct use of code:代码的正确使用:

struct Item: Codable, Identifiable {
    var id = UUID()
    let colors: [String]
}

struct ContentView: View {
    let item: Item
    
    init(item: Item){
        self.item = item
    }
    
    var body: some View {
        Circle()
            .fill(LinearGradient(gradient: Gradient(colors: item.colors.map{Color(hex: $0)}), startPoint: .leading, endPoint: .trailing))
    }
}

If you want to iterate your array by using forEach , you need to use the same as the old way.如果要使用forEach迭代数组,则需要使用与旧方法相同的方法。

Example:例子:

struct ContentView: View {
    let item: Item
    
    var gradientColors: [Color] = []
    
    init(item: Item){
        self.item = item
        item.colors.forEach { (strColor) in
            gradientColors.append(Color(hex: strColor))
        }
    }
    
    var body: some View {
        Circle()
            .fill(LinearGradient(gradient: Gradient(colors: gradientColors), startPoint: .leading, endPoint: .trailing))
    }
}

Also, one more thing.另外,还有一件事。 If you try to add a gradient to the circle, it's not work with .background(LinearGradient . For this you need to use .fill如果您尝试向圆圈添加渐变,则不适用于.background(LinearGradient 。为此,您需要使用.fill

var body: some View {
        Circle()
            .fill(LinearGradient(gradient: Gradient(colors: gradientColors), startPoint: .leading, endPoint: .trailing))
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM