benfernandes
benfernandes

Reputation: 179

Swift 3 - Run two functions at once

On the initial screen of the app I am creating, the user is presented with two options (register & sign in) which they must pressed to continue. In the middle of the screen is a logo and above it is the name of the app.

I want to make the background fade between multiple colours continuously but I'm having an issue that the buttons stop working when the colours are transitioning (which is all the time).

My current code:

override func viewDidAppear(_ animated: Bool) {
    randomColor()
}

@IBAction func registerButton(_ sender: UIButton) {
    print("New user is registering")
}

@IBAction func signInButton(_ sender: UIButton) {
    print("Returning user is signing in")
}

func randomColor() {
    let possibleColors = [standardRed, standardAqua, standardBlue, standardPink, standardGreen, standardOrange, standardPurple, standardYellow]
    let randomIndex = Int(arc4random_uniform(UInt32(possibleColors.count)))
    let randomColor = possibleColors[randomIndex]


    UIView.animate(withDuration: 3.0, delay: 0.0, options: .curveEaseInOut, animations: {
        self.view.backgroundColor = randomColor
    }, completion: { finished in
        self.randomColor()
    })
}

// 'standardXXX' are just global variable UIColors

Am I correct in thinking that because the function 'randomColor' keeps starting itself, no other functions can be started?

Are there any solutions so that 'randomColor()' can repeat itself while still allowing the other IBActions to be used?

Upvotes: 2

Views: 117

Answers (1)

Lawliet
Lawliet

Reputation: 3499

The reason is you didn't allow user interaction. Try replacing .curveEaseInOut by [.allowUserInteraction, .curveEaseInOut].

Upvotes: 2

Related Questions