Button swiftUI如何为2sec禁用按钮?

Button swiftUI如何为2sec禁用按钮?,button,swiftui,disable,Button,Swiftui,Disable,我有一个bug,如果我在动画前点击按钮,卡就会倒转回来。我认为对我来说最好是禁用按钮2秒,但我做了一些研究,没有发现任何东西 struct CardBack: View { var body: some View { Image("back_card") .resizable() .aspectRatio(contentMode: .fit) .frame(width: 250) } } struct Conte

我有一个bug,如果我在动画前点击按钮,卡就会倒转回来。我认为对我来说最好是禁用按钮2秒,但我做了一些研究,没有发现任何东西

struct CardBack: View {
    var body: some View {

      Image("back_card")
        .resizable()
        .aspectRatio(contentMode: .fit)
        .frame(width: 250)
    }
}  

struct ContentView: View {

   @State var flipped = false

   @State private var cardsFront = ["bigCard1", "bigCard2", "bigCard3", "bigCard4", "bigCard5" ]

   @State private var cardBack = "back_card"

    @State private var disablled = true

    var body: some View {
        VStack {
            Spacer()
            ZStack {

            Image(flipped ? self.cardsFront.randomElement()! : self.cardBack)
              .resizable()
              .aspectRatio(contentMode: .fit)
              .frame(width: 250)
              .rotation3DEffect(Angle(degrees: flipped ? 180 : 0 ), axis: (x: 0, y: 1, z: 0))
            }

            Spacer()
            HStack {
                Button(action: {
                    withAnimation(.spring()) {
                        self.flipped.toggle()
                    }
                    DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                        withAnimation(.spring()) {
                            self.flipped.toggle()
                        }
                    }

                }) {
                    Image("circle")
                        .renderingMode(.original)
                }


                Button(action: {

                }) {
                    Image("plus")
                        .renderingMode(.original)
                }
iOS 13,Swift 5

您可以先将按钮设置为禁用,然后使用我在这里使用的相同逻辑将其启用

导入快捷键

struct ContentView: View {
@State var changeColor = false

var body: some View {
    TextView(changeColor: $changeColor)
}
}

struct TextView: View {
@Binding var changeColor: Bool
var body: some View {

Text("Hello World")
    .foregroundColor(changeColor ? Color.black: Color.red)
    .onAppear {
        DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
            self.changeColor.toggle()
        }
    }
}
}
您就快到了,只需在代码中使用.appease标记即可