我正在制作一个测验应用程序。如果根据正确或错误的答案单击,其按钮颜色会发生变化。但我想在延迟一段时间后将颜色恢复到原来的颜色。延迟0.2秒后颜色改变但没有恢复默认。
这是我的代码:
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 score=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() {
super.viewDidLoad()
//fill the current question on screem
updateUI()
}
@IBAction func answerButtonPressed(_ sender: UIButton) {
// checking question not going to out of bound
if QuestionNumber + 1 < questionArray.count
{
let userAnswer = sender.currentTitle
let actualAnswer = questionArray[QuestionNumber].answer
if userAnswer == actualAnswer
{ // change the backgroundcolor
sender.backgroundColor=UIColor.green
score+=1
}
else
{ sender.backgroundColor=UIColor.red
score-=1
}
QuestionNumber+=1
// let some delay 0.2sec
Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(updateUI), userInfo: nil, repeats: false)
}
else
{
questionLabel.text="Done. Your score is \(score)"
print(score)
}
}
@objc func updateUI()
{
questionLabel.text=questionArray[QuestionNumber].text
trueButton.backgroundColor=UIColor.clear
falseButton.backgroundColor=UIColor.clear
print("color changed")
}
}
针对您的情况,一个非常简单的解决方案是使用 GCD 而不是 Timer: 只需更换这个:
Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(updateUI), userInfo: nil, repeats: false)
这样:
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
self.questionLabel.text = self.questionArray[QuestionNumber].text
sender = UIColor.clear
print("color changed")
}
在没有GCD的情况下解决问题是:
希望有帮助。