在Swift 3中 - 我试图在点击它之后将按钮的颜色改变一秒,这样用户就会得到一个指示是对还是错。好像我需要强制更新显示,但我不确定如何。
@IBAction func answer1(_ sender: UIButton) {
if (sender.tag == rightAnswerPlacement) {
sender.backgroundColor = UIColor.green
gmaeScore += 1
} else {
sender.backgroundColor = UIColor.red
gmaeScore -= 1
}
sleep(1)
score.text = "Score: \(gmaeScore)"
newQuestion()
}
func newQuestion() {
var firstResult: Int, secondResult: Int, thirdResult: Int, finalResult:Int
(firstResult, secondResult, thirdResult, finalResult) = generateQuestion()
timeCount = 0
firstAnswer.text = String(firstResult)
secondAnswer.text = String(secondResult)
thirdAnswer.text = String(thirdResult)
rightAnswerPlacement = generateNumber(range: 4, positive: true)
var button:UIButton = UIButton()
var alternativeAnswer = 0
//IUterate over the buttons
var allAnswers = Set<Int>()
allAnswers.insert(finalResult)
for i in 1...4 {
//Converting each of the buuton tags into a button
button = view.viewWithTag(i) as! UIButton
button.backgroundColor = UIColor.blue
if (i == Int(rightAnswerPlacement)){
button.setTitle(String(finalResult), for: .normal)
} else {
var myRange = finalResult * 2
if (myRange < 0) {
myRange *= -1
}
alternativeAnswer = generateNumber(range: myRange)
while allAnswers.contains(alternativeAnswer) {
alternativeAnswer = generateNumber(range: myRange)
}
allAnswers.insert(alternativeAnswer)
button.setTitle(String(alternativeAnswer), for: .normal)
}
}
答案 0 :(得分:1)
@IBAction func answer1(_ sender: UIButton) {
if (sender.tag == rightAnswerPlacement) {
UIView.animate(withDuration: 0.5, animations: {
sender.backgroundColor = UIColor.green
},completion: { _ in
sender.backgroundColor = UIColor.green //change it back to original color
})
gmaeScore += 1
} else {
UIView.animate(withDuration: 0.5, animations: {
sender.backgroundColor = .red
},completion: { _ in
sender.backgroundColor = UIColor.green //change it back to original color
})
gmaeScore -= 1
}
sleep(1)
score.text = "Score: \(gmaeScore)"
newQuestion()
}
完成程序段将在您的持续时间后执行:)
答案 1 :(得分:0)
你可以试试这个
@IBAction func answer1(_ sender: UIButton) {
UIView.animate(withDuration: 1) {
sender.backgroundColor = .green
sender.backgroundColor = .red
}
}