【问题标题】:How to change color of buttons created dynamically inside forEach in SwiftUI如何更改在 SwiftUI 中的 forEach 中动态创建的按钮的颜色
【发布时间】:2020-07-05 21:24:46
【问题描述】:

我想在 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)
        }
    }
}

【问题讨论】:

  • 你能提供你的代码吗?
  • 我已经更新了描述和代码。请检查@Asperi
  • @RoshanChamagain 更改按钮颜色时不应更改其他按钮的颜色是什么意思?目前,当您选择一个按钮时,您会将所有选定按钮的颜色更改为某种颜色。您只想更改一个按钮的颜色吗?你如何选择颜色?请详细说明您的预期行为。
  • @pawello2222 实际上我想将按钮着色为上面的颜色数组。例如:如果用户首先选择任何按钮,那么该按钮的颜色应该是有机的,如果用户选择另一个按钮,那么它应该是绿色等等。用户最多可以选择 7 个按钮,如果选择了 7 个不同的按钮,那么它们应该有 7 种不同的颜色。上面代码中的问题是,如果用户首先选择任何按钮,则该按钮的颜色为橙色,如果选择第二个按钮,则两者都变为绿色。谢谢:)
  • @pawello2222 我已作为单独的问题发布。 link

标签: ios swiftui


【解决方案1】:

您可以尝试以下方法:

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)
        }
    }
}

【讨论】:

  • 我又来了,如果有20个按钮,我只能选择5种颜色怎么办?上面的代码仅在按钮和颜色计数相同时才有效。谢谢
  • @RoshanChamlagin 这是一个不同的问题。请创建另一个问题并描述您的新问题 - 我很乐意为您提供帮助 :)
猜你喜欢
  • 1970-01-01
  • 2019-10-29
  • 1970-01-01
  • 1970-01-01
  • 2020-01-19
  • 2020-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多