Ios 在SwiftUI中单击forEach语句中的按钮时,如何更改不同颜色按钮的背景?

Ios 在SwiftUI中单击forEach语句中的按钮时,如何更改不同颜色按钮的背景?,ios,swift,xcode,swiftui,swift5,Ios,Swift,Xcode,Swiftui,Swift5,我想把按钮涂成上面的颜色数组。例如:如果用户首先选择任何按钮,则该按钮的颜色应为橙色,如果用户选择另一个按钮,则该按钮应为绿色,依此类推。用户可以从总共10个按钮中选择多达7个按钮,如果选择了7个不同的按钮,则它们应具有7种不同的颜色 import SwiftUI struct ColorModel: Identifiable { let value: Color let id = UUID() } let colors = [ ColorModel(value: Co

我想把按钮涂成上面的颜色数组。例如:如果用户首先选择任何按钮,则该按钮的颜色应为橙色,如果用户选择另一个按钮,则该按钮应为绿色,依此类推。用户可以从总共10个按钮中选择多达7个按钮,如果选择了7个不同的按钮,则它们应具有7种不同的颜色

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 {
    @State private var selectedButtons = [Int]()

    var body: some View {
        ForEach(0..<totalButtons) { index in
            Button(action: {
                self.updateSelectButton(value: index)
            }) {
                Text("Button \(index)")
            }
            .background(self.selectedButtons.contains(index) ? colors[index].value : Color.white)
        }
    }

    func updateSelectButton(value: Int) {
        guard value < colors.count else {
            return
        }
        if let index = self.selectedButtons.firstIndex(of: value) {
            self.selectedButtons.remove(at: index)
        } else {
            self.selectedButtons.append(value)
        }
    }
}
导入快捷界面
结构颜色模型:可识别{
让值:颜色
设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{
@状态私有变量selectedButtons=[Int]()
var body:一些观点{

ForEach(0..您需要删除这行代码。由于这行代码,用户无法选择数组中的第8、第9和第10个按钮

   guard value < colors.count else {
        return
    }

}

您需要删除这行代码。由于这行代码,用户无法选择数组中的第8、第9和第10个按钮

   guard value < colors.count else {
        return
    }

}您可以尝试以下方法:

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

    var body: some View {
        ForEach(0 ..< totalButtons) { index in
            Button(action: {
                self.updateSelectButton(value: index)
            }) {
                Text("Button \(index)")
            }
            .background(self.buttonColor(value: index)) // <- extract to another function for clarity
        }
    }

    func updateSelectButton(value: Int) {
        if let index = selectedButtons.firstIndex(of: value) {
            selectedButtons.remove(at: index)
        } else if selectedButtons.count < 7 { // <- make sure we never go above 7
            selectedButtons.append(value)
        }
    }
    
    func buttonColor(value: Int) -> Color {
        if let index = selectedButtons.firstIndex(of: value), index < colors.count { // <- safety check
            return colors[index].value
        } else {
            return .white
        }
    }
}
struct ContentView:View{
@状态私有变量selectedButtons=[Int]()
var body:一些观点{
ForEach(0...background(self.buttonColor(value:index))/您可以尝试以下操作:

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

    var body: some View {
        ForEach(0 ..< totalButtons) { index in
            Button(action: {
                self.updateSelectButton(value: index)
            }) {
                Text("Button \(index)")
            }
            .background(self.buttonColor(value: index)) // <- extract to another function for clarity
        }
    }

    func updateSelectButton(value: Int) {
        if let index = selectedButtons.firstIndex(of: value) {
            selectedButtons.remove(at: index)
        } else if selectedButtons.count < 7 { // <- make sure we never go above 7
            selectedButtons.append(value)
        }
    }
    
    func buttonColor(value: Int) -> Color {
        if let index = selectedButtons.firstIndex(of: value), index < colors.count { // <- safety check
            return colors[index].value
        } else {
            return .white
        }
    }
}
struct ContentView:View{
@状态私有变量selectedButtons=[Int]()
var body:一些观点{
ForEach(0...background(self.buttonColor(value:index))//用户可以从总共10个按钮中选择最多7个按钮这意味着在任何时候都不应该选择超过7个按钮,对吗?如果您只选择一个按钮(比如第10个),它应该有哪种颜色?如果您只选择第10个按钮,那么它应该是橙色的(颜色数组中的第一项)如果选择,比如说第四个按钮,那么第四个按钮应该是绿色的,依此类推。@Pawello2222用户可以从总共10个按钮中选择最多7个按钮,这意味着在任何时候都不应该选择超过7个按钮,对吗?如果您只选择一个按钮(比如第10个)它应该有哪种颜色?如果只选择第10个按钮,那么它应该有橙色(颜色数组中的第一项)如果选择第四个按钮,则第四个按钮应为绿色,依此类推。@Pawello2222如果我删除这一行,则代码将中断,因为颜色数组有7项,值可以从0到9。@ChamanSharmaUse这一点-if self.selectedButtons.count>=7{return}如果我删除这一行,代码将中断,因为颜色数组有7个项目,值可以从0到9。@ChamanSharmaUse这个-If self.selectedButtons.count>=7{return}