English 中文(简体)
如何参考一个在座 to中创建的纽顿
原标题:How to reference a button created in a foreach loop

如何参照每处一个 lo子里设立的纽顿

我有以下法典,其中我对使用“ForEach loop”创建的一组纽芬兰州适用了一种习俗。 我试图做到两点: 当一个县受到压力时,1台。 如果是正确的纽扣吨,(即与用户匹配)其边界闪光在产生新的用户需求之前是绿色的(我已设法做到大部分), 2. 如果是错的纽顿,那么与用户即时闪电相匹配的纽顿就向用户表示应该受到压力。

I’ve tried two different ways of creating this effect. Firstly using a function that triggers when the button is pressed, passing in the buttons border colour, but this didn’t work as it didn’t allow me to specify which button should flash red in the case of a wrong press. Now I’ve been trying to do it via custom buttonStyle, but this is similar in practice except it does a lot more of what I want.

TLDR ;从本质上讲,我有麻烦地把一个由每个娱乐场所创建的纽州的一个单一案例提到。 我的想象力,我需要把第二个条件列入我的检查条件,即“Ifutton与用户匹配,然后又闪光了那顿红色”。 但是,不能确定如何做到这一点。

这里,我迄今为止已经做了以下工作:

import SwiftUI
struct ContentView: View {
    var fullArray = ["1","2","3","4","5","6"]
    var topArray = ["1","2","3"]
    var botArray = ["4","5","6"]
    @State private var userPrompt = ["1","2","3","4","5","6"]
    @State private var newLetter = ""
    @State private var borderColor = Color.black
    @State private var letterTapped = ""
    func chosenLetter() -> String {
        return userPrompt.randomElement() ?? "Not working"
    }
    
    var body: some View {
        VStack{
            Text(newLetter)
                .padding()
                .font(.title)
                .background(Color.blue)
                .foregroundColor(.white)
            HStack {
                
                ForEach(topArray, id: .self) { topLetter in
                    Button(action: {
                        print("Top number keys")
                    }){
                        Text(topLetter)
                    }
                    .buttonStyle(CustomButtonStyle(keyLabel: topLetter, newLetter: $newLetter, borderColor: $borderColor))

                }
                
            }
            HStack{
                ForEach(botArray, id: .self){ botLetter in
                    Button(action:{
                        print("Bottom number keys")
                    }){
                        Text(botLetter)
                    }
                    .buttonStyle(CustomButtonStyle(keyLabel: botLetter, newLetter: $newLetter, borderColor:  $borderColor))
                }
            }
            .padding()
        }
        .onAppear {
          newLetter = chosenLetter()
        }
    }
    struct CustomButtonStyle: ButtonStyle {
        let keyLabel: String
        @State var isPressed = false
        @Binding var newLetter : String 
        @Binding public var borderColor: Color
//        @Binding var correctLetter : Bool
        
        func chooseBorder() -> Color {
            if keyLabel == newLetter {
                if isPressed{
                    return Color.green
                }
            }else{
                return Color.black
            }
            return Color.black
        }
        
        func makeBody(configuration: Configuration) -> some View {
            configuration.label
                .padding()
                .border(chooseBorder())

                .onTapGesture {
                    isPressed = true
                    // Change border color if label matches userPrompt
                    if keyLabel == newLetter {
                        borderColor = .green
                        DispatchQueue.main.asyncAfter(deadline: .now() + 0.4)
                        {
                            borderColor = Color.black
                        }

                    }else{
                        borderColor = .red
                            DispatchQueue.main.asyncAfter(deadline: .now() + 0.4)
                            {
                                borderColor = Color.black
                            }    
                    }
                }
        }
    }
}

I have tried completing this as a function that runs in the buttons action instead of through a custom button style. One idea I’ve had is that I could generate a dictionary that has each string and an associated value. This might then allow me to reference that button based on its value. But I think I might be over complicating it

问题回答

。 不应成为<条码>@State。 View。 每个州都有自己的边界。 事实上,每个纽伦州都需要一个<条码>,即作为国家。 纽特州边界图由<代码>chooseBorder确定,该编码取决于keyLetter/code>、newLetterisPressed。 应如此执行:

func chooseBorder() -> Color {
    if keyLabel == newLetter && isPressed {
        return Color.green
    } else if isPressed {
        return Color.red
    }
    return Color.black
}

在0.4秒后,不设置<条码> 跨界Color,而是将<条码>改为伪造。

.onTapGesture {
    isPressed = true
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.4)
    {
        isPressed = false
    }
    if keyLabel == newLetter {
        newLetter = // choose a new letter...
    }
}

我确实认为,尽管如此,你却不然。 您仅能创建一种惯例,即<条码>。

struct CustomButton: View {
    let keyLabel: String
    let newLetter: String
    let action: () -> Void
    @State var borderColor = Color.black
    @State var flashTrigger = false
    
    var body: some View {
        Text(keyLabel)
            .padding()
            .border(borderColor)
            .onTapGesture {
                flashTrigger.toggle()
            }
            .task(id: flashTrigger) {
                borderColor = keyLabel == newLetter ? .green : .red
                do {
                    try await Task.sleep(for: .milliseconds(400))
                } catch {
                    return
                }
                borderColor = .black
                if keyLabel == newLetter {
                    action()
                }
            }
    }
}

使用:

HStack {
    ForEach(topArray, id: .self) { topLetter in
        CustomButton(keyLabel: topLetter, newLetter: newLetter) {
            newLetter = chosenLetter()
        }
    }
}
HStack{
    ForEach(botArray, id: .self){ botLetter in
        CustomButton(keyLabel: botLetter, newLetter: newLetter) {
            newLetter = chosenLetter()
        }
    }
}

今天,我们将向你们展示如何落实“ty进展”概念。 这一概念是科林·加文的布尔特顿。 先研究一下,了解需要采取哪些步骤,并了解情况。 纽顿背后的想法如下:一旦被点击,提交纽特就会变成一个循环,显示使用边界进行的进展估计。 如科林在一份评论中提及的那样,如果进展预测完成,则纽伦将再次扩大,并显示一份核对表,确认划界案已经完成。 我们正在落实这一概念,并在呈文失败时为案件增添另一个条件。





相关问题
C# Building An Array with Button Entries

I have a set of buttons that I want to add into an array so that they are ordered. The buttons I have are: Monday0700Button Monday0730Button Monday0800Button Monday0830Button and so on. How do I ...

Text in disabled and css styled button shows movement in IE

I have disabled and css styled buttons. In Firefox, disabled buttons do not move at all when pressed (which is what is expected), in IE, the text in the buttons still moves a few pixels. Why does ...

Where is Button.DialogResult in WPF?

In System.Windows.Forms.Button there is a property DialogResult, where is this property in the System.Windows.Controls.Button (WPF)?

How to create a submit button template in Oracle APEX?

I m trying to create a template for a button in Oracle APEX but I don t seem to have access to the appropriate substitution strings to make it work. For non-templated buttons APEX seems to insert a ...

Removing text from HTML buttons on all browsers?

We have buttons of many sizes and colors that use background images. There is a label on the background image itself, but we need to keep the button s text in the HTML for usability/accessibility. How ...

Back button loop with IFRAMES

In my (school) website we use Iframes to display class blogs (on blogger). This works well EXCEPT if the user then clicks on (say) a photo inside the iframe. Blogger (in this case) then displays the ...

ruby on rails on click events

I am so sorry if this question seems too easy but I can t seem to find it anywhere. I am creating a ruby in steel project. I have created a html.erb and rb file in a vs project (ruby on rails). My ...

热门标签