繁体   English   中英

Swift:按下按钮时更改 UIButton 背景颜色?

[英]Swift: Change UIButton backgroundColor when button is pressed?

我正在尝试创建一个由两个按钮(假按钮和真按钮)组成的测验应用程序。 我的问题是,当我按下两个按钮之一时,我希望它在按下时仅在短时间内更改其背景颜色,然后我希望它 go 恢复到原来的颜色,但我不知道如何更改背景颜色很快。 到目前为止,这是我对这部分的代码:

 @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 }

你可以试试

// 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
    }
  }
}

使用它

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

并改变

updateUI()

DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
  self.updateUI()
}

这是 UIButton 分机,带有 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
        }
    }
}

采用:

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

这将帮助您将按钮颜色更改为您想要的颜色(绿色或红色或其他颜色)并延迟 0.2 秒将其更改回清除。 以最简单的方式。

在答案检查完成后,在 answerButtonPressed 的 IBAction 中放置以下行。

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

在选择器中,指定用于更新用户界面的 function。

我正在尝试创建一个包含两个按钮的测验应用程序,即 false 和 true 按钮。 我的问题是,当我按下两个按钮之一时,我希望它仅在按下它的时候改变它的背景颜色,然后我希望它 go 回到原来的颜色,但我不知道如何改变背景颜色很快。 这是我到目前为止这部分的代码:

 @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 }

在这种情况下,以下将是最快和最简单的解决方案:

只需更改您的 'updateUI ()' function 如下:

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

那么这里发生了什么?

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

在将背景更改回清晰颜色之前,上面的代码行将绿色/红色保持 0.3 秒(您可以根据自己的喜好更改此数字)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM