1

I'm trying to create a Quiz app that consists of two buttons, false and true button. My question is, when I press one of the two buttons, I want it to change its background colour ONLY SHORTLY when its pressed then I want it to go back to the color it originally was, but I can not figure out how to change the background color shortly. This is the code I have for this part so far:

@IBAction func answerButtonPressed(_ sender: UIButton) {
        
        let userAnswer = sender.currentTitle
        let actualAnswer = quiz[questionNumber].answer
        
        
        if userAnswer == actualAnswer {
            sender.backgroundColor = UIColor.green
        } else {
            sender.backgroundColor = UIColor.red
         }
        
        if questionNumber + 1 < quiz.count {
        
        questionNumber += 1
            }
        else {
            questionNumber = 0
        }
        updateUI()
        
        
        }
        
    func updateUI() {
        questionLabel.text = quiz[questionNumber].text
        trueButton.backgroundColor = UIColor.clear
        falseButton.backgroundColor = UIColor.clear

    

}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
Mervan
  • 27
  • 1
  • 8
  • Does this answer your question? [Change button background color using swift language](https://stackoverflow.com/questions/24427284/change-button-background-color-using-swift-language) – Vitalii Gozhenko Dec 15 '20 at 11:03

4 Answers4

4

You can try

// add this code snippet outside of any class 
extension UIButton {
  func shortChangeTo(_ color:UIColor) {
    let prev = self.backgroundColor
    self.backgroundColor = color
    DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
       self.backgroundColor = prev
    }
  }
}

to use it

if userAnswer == actualAnswer {
   sender.shortChangeTo(.green)
} else {
   sender.shortChangeTo(.red)
}

And change

updateUI()

to

DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
  self.updateUI()
}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
1

Here is the UIButton extension with the change background for short time color with animation.

extension UIButton {
    func shortChangeBackground(with color: UIColor) {
        let originalColor = self.backgroundColor
        
        UIView.animate(withDuration: 0.3) {
            self.backgroundColor = color
        }
        
        UIView.animate(withDuration: 0.3, delay: 1.0) {
            self.backgroundColor = originalColor
        }
    }
}

Use:

@IBAction func onDoneAction(_ sender: UIButton) {
    sender.shortChangeBackground(with: userAnswer == actualAnswer ? UIColor.green : UIColor.red)
}

Raja Kishan
  • 16,767
  • 2
  • 26
  • 52
0

This will help you to change the button colour to colour you want (green or red or whatever) and change it back to clear with delay of 0.2 seconds. In the easiest way possible.

In the IBAction of answerButtonPressed put the following lines after the answer check is done.

Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(updateUI), userInfo: nil, repeats: false)

In selector, specify your function used to update the user interface.

0

In this situation, the following would be the fastest and simplest solution:

Just change your 'updateUI ()' function as follows:

func updateUI() {
        questionLabel.text = quiz[questionNumber].text
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
            self.trueButton.backgroundColor = UIColor.clear
            self.falseButton.backgroundColor = UIColor.clear
        }
        
    }

So what's happening here?

DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) 

The above line of code is holding off green/red color for 0.3 secs (you can change this number to your preference) before changing the background back to clear color.

Dharman
  • 30,962
  • 25
  • 85
  • 135