【问题标题】:How can I select only one button from a `ForEach`?如何从“ForEach”中只选择一个按钮?
【发布时间】:2021-03-26 15:43:39
【问题描述】:

我有一些数据并完成了ForEach 以将它们写入按钮。然后我有一个onLongPressGesture,我想在长按时更改特定按钮的背景颜色。我怎样才能实现它?

这是我的代码。

@State var selectedSummaryOption = "Daily"
    var hi = false
    var summaryOptions:[String] = ["Daily", "Weekly", "Monthly"]
    @State var prayerTimesImage = [
        prayerTimesImages(name: "Fajr", isActive: false),
        prayerTimesImages(name: "Zuhr", isActive: false),
        prayerTimesImages(name: "Asr", isActive: false),
        prayerTimesImages(name: "Maghrib", isActive: false),
        prayerTimesImages(name: "Isha", isActive: false)]
VStack {
                                    ForEach(prayerTimesImage, id: \.id) {prayerIcon in
                                        Button(action: {}) {
// HS -- Content of Button
                                            HStack {
                                                Image(prayerIcon.name)
                                                    .resizable()
                                                    .aspectRatio(contentMode: .fit)
                                                    .frame(width: 50, height: 50)
                                                Text("\(prayerIcon.name)")
                                                Spacer()
                                            }
                                            .padding(.horizontal, 5)
                                            .padding(.vertical, 10)
                                            .background(prayerIcon.isActive ? Color.green : Color.white)
                                            //.scaleEffect(longPressed ? 0.98 : 1)
                                            .onLongPressGesture(minimumDuration: 1) {
                                                prayerIcon.isActive = true   //gives error
                                                print(prayerIcon.isActive)
                                            }
                                            
                                        }.cornerRadius(10)
                                        .shadow(radius: 2)}
                                        .font(.system(.headline))
                                        .foregroundColor(.black)
                            }
                        }.frame(width: totalWidth, alignment: .center)

【问题讨论】:

    标签: swift swiftui gesture


    【解决方案1】:

    prayerIcon 是一个副本,这就是您收到错误的原因。使用enumerated 直接在状态属性容器中同时拥有索引和项目并更改模型,例如

    ForEach(Array(prayerTimesImage.enumerated()), id: \.1.id) { (index, prayerIcon) in
    
         ...
    
        .onLongPressGesture(minimumDuration: 1) {
            prayerTimesImage[index].isActive = true   // << change in model
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-17
      • 2016-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-23
      • 2011-08-13
      • 2015-08-30
      相关资源
      最近更新 更多