【问题标题】:swiftUI how to disabled a button for 2sec?swiftUI如何禁用按钮2秒?
【发布时间】:2020-05-31 08:06:22
【问题描述】:

我有一个错误,如果我在卡片翻转之前在动画之前单击按钮。我认为对我来说最好的方法是禁用按钮 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)
                }

【问题讨论】:

    标签: button swiftui disable


    【解决方案1】:

    iOS 13、斯威夫特 5

    您可以将按钮最初设置为禁用,然后使用我在此处使用的相同逻辑启用它。

    导入 SwiftUI

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

    您就快到了,您只需要在代码中使用 .appear 标记即可。

    【讨论】:

      猜你喜欢
      • 2023-04-04
      • 1970-01-01
      • 2022-01-23
      • 2023-03-23
      • 2022-01-08
      • 2012-01-06
      • 2020-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多