简体   繁体   中英

Swift 4 - while loops and UI elements

-- Edit --

I cannot answer my own question, but I will leave my solution here in this edit.

Thanks everyone for your time in looking at my problem.

I am creating 1 new variable

var timer = 1

I am using this in my Async dispatch timer for the seconds, and incrementing it by 1 each round to space the sequence out, it took a lot of thinking, but this solves my problem.

I wanted the buttons to be played in a sequential order, the solution then is to adjust the dispatch timer per the below.

Thank you everyone that commented, it got me thinking differently and helped me to solve the problem.


I am new to programming and to Swift and have been trying to make a small program but I have been stumped on a problem with loops, I am really new to swift and have tried "for loop", repeat-while, while loops, etc. The issue I have is I am trying to get 4 UI elements "Alphas" to change in the order that a new value is created but currently it seems to happen so fast that all 4 buttons just blink out at the same time.

What I am doing is a there are 4 colored tiles, I am using single value array generated randomly and on each pass of the loop I am appending the newly generated array value of "newChallenge" to an array called "challenge" which the user will need to input to get a correct result from the game.

I was trying to add playback and there is a function called "showChallenge", I convert the newChallenge value to an integer and use a switch hoping that each of the buttons would flash in order of the newChallenge being created. However what I am getting is that all 4 tiles will just flash at once, I have tried using async to create a timing between them but I am missing something. Can someone let me know how to approach this or show me where I am going wrong in my code?

there are a few other variables but they are self explanatory, if someone could help me out it would be great. This is my first Swift program, and as fun as it has been I have spent a lot of hours trying to solve this problem. I would appreciate any help I can get :)

func challenges() {
    var low = 1
    var high = 5

    while low != high {
        newChallenge = [Int.random(in: 1..<5)]
        showChallenge()
        challenge.append(contentsOf: newChallenge)
        low += 1
     }
     tmpChallenge.text = "Array: \(self.challenge)" //debugging
}

func showChallenge () {
    DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
        self.uTurnPrompt.isHidden = false
        self.low = 1
    }        
    var btnFeedback = newChallenge[indexElement]
    uTurnPrompt.isHidden = true

    switch ("JRSM" == "JRSM") {
        case btnFeedback == 1:
            print("num1")
            self.btn_Num1.alpha = 0.1
            DispatchQueue.main.asyncAfter(deadline: .now() + (btnSpeed)) {
                self.btn_Num1.alpha = 1
            }
            break;
        case btnFeedback == 2:
            print("num2")
            self.btn_Num2.alpha = 0.1
            DispatchQueue.main.asyncAfter(deadline: .now() + (btnSpeed)) {
                self.btn_Num2.alpha = 1
            }
            break;
        case btnFeedback == 3:
            print("num3")
            self.btn_Num3.alpha = 0.1
            DispatchQueue.main.asyncAfter(deadline: .now() + (btnSpeed)) {
                self.btn_Num3.alpha = 1
            }
            break;
        case btnFeedback == 4:
            print("num4")
            self.btn_Num4.alpha = 0.1
            DispatchQueue.main.asyncAfter(deadline: .now() + (btnSpeed)) {
                self.btn_Num4.alpha = 1
            }
            break;
        default:
            break;
        }

    infoText.isHidden = false
    DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
        self.infoText.isHidden = true
    }
}

The issue is that you're calling showChallenge in the loop. Everything is called synchronously and so it's going to appear that it's all being called at the same time.

Modify calling showChallenge in the loop by wrapping it in a delay based on the order you're calling it, like this:

DispatchQueue.main.asyncAfter(deadline: .now() + low) {
    showChallenge()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM