Ios 如何在SwiftUI中更改forEach中动态创建的按钮的颜色

Ios 如何在SwiftUI中更改forEach中动态创建的按钮的颜色,ios,swiftui,Ios,Swiftui,我想在swiftUI forEach语句中使用不同颜色更改不同按钮的颜色。更改按钮颜色时,不应更改其他按钮的颜色。我怎样才能做到这一点?我的代码如下所示: import SwiftUI struct ColorModel: Identifiable { let value: Color let id = UUID() } let colors = [ ColorModel(value: Color.orange), ColorModel(value: Color

我想在swiftUI forEach语句中使用不同颜色更改不同按钮的颜色。更改按钮颜色时,不应更改其他按钮的颜色。我怎样才能做到这一点?我的代码如下所示:

import SwiftUI

struct ColorModel: Identifiable {
    let value: Color
    let id = UUID()
}
let colors = [
    ColorModel(value: Color.orange),
    ColorModel(value: Color.green),
    ColorModel(value: Color.blue),
    ColorModel(value: Color.red),
    ColorModel(value: Color.yellow),
    ColorModel(value: Color.gray),
    ColorModel(value: Color.pink),
]
let totalButtons: Int = 10

struct ContentView: View {
    func updateSelectedButtons(value: Int) {
        if self.selectedButtons.contains(value) {
            if let index = self.selectedButtons.firstIndex(of: value) {
                self.selectedButtons.remove(at: index)
            }
        } else {
            if self.selectedButtons.count < 7 {
                self.selectedButtons.append(value)
            }
        }
    }
    @State private var selectedButtons: [Int] = [Int]()
    @State private var colorIndex: Int = 0
    var body: some View {
        ForEach(0 ..< totalButtons) { index in
            Button(action: {
                self.updateSelectedButtons(value: index)
                self.colorIndex += 1
            }) {
                Text("  ")
            }
            .background(self.selectedButtons.contains(index) ? colors[self.colorIndex].value : Color.white)
        }
    }
}
导入快捷界面
结构颜色模型:可识别{
让值:颜色
设id=UUID()
}
让颜色=[
ColorModel(值:Color.orange),
ColorModel(值:Color.green),
ColorModel(值:Color.blue),
ColorModel(值:Color.red),
ColorModel(值:Color.yellow),
ColorModel(值:Color.gray),
ColorModel(值:Color.pink),
]
让totalButtons:Int=10
结构ContentView:View{
func updateSelectedButtons(值:Int){
如果self.selectedButtons.contains(值){
如果let index=self.selectedButtons.firstIndex(of:value){
self.selectedButtons.remove(在:索引处)
}
}否则{
如果self.selectedButtons.count<7{
self.selectedButtons.append(值)
}
}
}
@国家私有变量selectedButtons:[Int]=[Int]()
@国家私有变量colorIndex:Int=0
var body:一些观点{
ForEach(0..
您可以尝试以下方法:

struct ContentView: View {
    @State private var selectedButtons = [Int]()

    var body: some View {
        ForEach(0..<totalButtons) { index in
            Button(action: {
                self.updateSelectButton(value: index) // <- on tap update selection
            }) {
                Text("Button \(index)")
            }
            .background(self.selectedButtons.contains(index) ? colors[index].value : Color.white) // <- if index is selected set color 
        }
    }

    func updateSelectButton(value: Int) {
        guard value < colors.count else { // <- make sure you don't go outside the `colors` range
            return
        }
        if let index = self.selectedButtons.firstIndex(of: value) {
            self.selectedButtons.remove(at: index)
        } else {
            self.selectedButtons.append(value)
        }
    }
}
struct ContentView:View{
@状态私有变量selectedButtons=[Int]()
var body:一些观点{

弗雷奇(0..你能提供你的代码吗?我已经更新了描述,代码在那里。请检查@Asperi@RoshanChamlagain更改按钮颜色时不应更改其他按钮的颜色是什么意思?当前,当您选择一个按钮时,会将所有选定按钮的颜色更改为某种颜色。是否只更改颜色我们只选择了一个按钮?您如何选择颜色?请详细说明您的预期行为。@pawello2222实际上我想将按钮的颜色设置为上面的颜色数组。例如:如果用户首先选择任何按钮,则该按钮的颜色应为organe,如果用户选择了另一个按钮,则该按钮的颜色应为绿色,依此类推。使用r最多可以选择7个按钮,如果选择了7个不同的按钮,则它们应具有7种不同的颜色。上面代码中的问题是,如果用户首先选择任何按钮,则该按钮的颜色为橙色,如果选择第二个按钮,则两者都变为绿色。谢谢:)@pawello2222我已经发布了一个单独的问题。我又来了,如果有20个按钮,我只能选择5种颜色怎么办?上面的代码只有在按钮和颜色计数相同的情况下才有效。Thanks@RoshanChamlagain这是一个不同的问题。请创建另一个问题并描述您的新问题-我很乐意帮助您:)