Friday, April 26, 2024
HomeiOS Developmentios - UIButton colour not restored to authentic colour

ios – UIButton colour not restored to authentic colour


I’m making a quiz app. Its button color adjustments if clicked based mostly on right or fallacious reply. However I need to restore the colour to the unique one after some delay. The colour is modified however not restored to the default after the delay of 0.2 seconds.

Right here is my code:

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var questionLabel: UILabel!
    @IBOutlet weak var progressBar: UIProgressView!
    @IBOutlet weak var falseButton: UIStackView!
    @IBOutlet weak var trueButton: UIStackView!

    var QuestionNumber=0
    var rating=0
    var questionArray = [
        Question(q: "A slug's blood is green.", a: "True"),
        Question(q: "Approximately one quarter of human bones are in the feet.", a: "True"),
        Question(q: "The total surface area of two human lungs is approximately 70 square metres.", a: "True"),
        Question(q: "In West Virginia, USA, if you accidentally hit an animal with your car, you are free to take it home to eat.", a: "True"),
        Question(q: "In London, UK, if you happen to die in the House of Parliament, you are technically entitled to a state funeral, because the building is considered too sacred a place.", a: "False"),
        Question(q: "It is illegal to pee in the Ocean in Portugal.", a: "True"),
        Question(q: "You can lead a cow down stairs but not up stairs.", a: "False"),
        Question(q: "Google was originally called 'Backrub'.", a: "True"),
        Question(q: "Buzz Aldrin's mother's maiden name was 'Moon'.", a: "True"),
        Question(q: "The loudest sound produced by any animal is 188 decibels. That animal is the African Elephant.", a: "False"),
        Question(q: "No piece of square dry paper can be folded in half more than 7 times.", a: "False"),
        Question(q: "Chocolate affects a dog's heart and nervous system; a few ounces are enough to kill a small dog.", a: "True")
    ]

    override func viewDidLoad() {
        tremendous.viewDidLoad()
        //fill the present query on screem
        updateUI()
    }
    
    @IBAction func answerButtonPressed(_ sender: UIButton) {
        // checking query not going to out of certain
        if QuestionNumber + 1 < questionArray.rely
        {
            let userAnswer = sender.currentTitle
            
            let actualAnswer = questionArray[QuestionNumber].reply
           
            if userAnswer == actualAnswer
            {   // change the backgroundcolor
                sender.backgroundColor=UIColor.inexperienced
                rating+=1
            }
            else
            {   sender.backgroundColor=UIColor.purple
                score-=1
            }
            QuestionNumber+=1
            // let some delay  0.2sec 
            Timer.scheduledTimer(timeInterval: 0.2, goal: self, selector: #selector(updateUI), userInfo: nil, repeats: false)
           
        }
       else
        {
           questionLabel.textual content="Carried out. Your rating is (rating)"
           print(rating)
          
       }
    }

    @objc func updateUI()
    {
        questionLabel.textual content=questionArray[QuestionNumber].textual content
        trueButton.backgroundColor=UIColor.clear
        falseButton.backgroundColor=UIColor.clear
        
        print("colour modified")
    }
}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments