Button Disabled But I can click it Swiftui

118 views Asked by At

I'm trying to do a Playground project in XCode, what I want to do is this type of Button "Next": when I click it, it should disappear and wait till it will be abled. In my program it disappear correctly, but if I click in the position where it should be, it seems as if I clicked it, even if it is not there.

The part of the code where I'm trying to do this is:

           if(isButtonEnabled){
               let numButtons = story.getAllOrdered()[index].responses.count
               HStack(spacing: 50.0){
                   ForEach(0..<numButtons){ i in
                       let buttonFields = story.getAllOrdered()[index].responses[i]
                       
                       Button(action: nextText, label: {
                           Text(buttonFields.text)
                               .foregroundColor(Color(buttonFields.textColor))
                               .frame(width: UIScreen.main.bounds.size.width * 0.3)
                               .font(MyFont.loadFont(size: 35, name: "Grenze-Bold"))
                       })
                       .padding()
                       .background(.cyan)
                       .cornerRadius(10)
                       .disabled(!isButtonEnabled)
                   }
               }.padding()
           }

and the function of the button is:

    func nextText() {
        if(index + 1 == story.getAllOrdered().count){
            // It's not relevant for the problem
            seeMe()
        }else{
            index = (index + 1) % story.getAllOrdered().count
            textToShow = ""
            isButtonEnabled = false

            // It's not relevant for the problem
            self.timer = Timer.publish(every: 0.05, on: .main, in: .common).autoconnect()
        }
    }

And this the text that enable the button after it is finished:

      Text(textToShow)
          .lineLimit(nil)
          .fixedSize(horizontal: false, vertical: true)
          .foregroundColor(.white)
          .font(MyFont.loadFont(size: 35, name: "Grenze-Regular"))
          .frame(width: UIScreen.main.bounds.size.width * 0.8, alignment: .leading)
          .padding()
          .onReceive(timer) { _ in
                           
              let text = story.getAllOrdered()[index].string
                                
              let textIndex = textToShow.index(textToShow.startIndex, offsetBy: textToShow.count)

                   if(textIndex < text.endIndex){
                     textToShow += String(text[textIndex])
                                    
                   }else{
                     timer.upstream.connect().cancel()
                     isButtonEnabled = true
                   }
          }          

I tried to change the boolean condition but of course it doesn't work. I figured out that this issue happens only when I have 2 buttons, and not 1. Maybe this could be helpful.

0

There are 0 answers