【问题标题】:Swift: Change UIButton backgroundColor when button is pressed?Swift:按下按钮时更改 UIButton backgroundColor?
【发布时间】:2021-03-25 22:46:05
【问题描述】:

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

@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

    

}

【问题讨论】:

标签: ios swift uibutton background-color


【解决方案1】:

你可以试试

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

【讨论】:

  • 能否请您告诉我如何在我的代码 sn-p 中实现这一点。我是初学者:D
  • 请注意,您应该在 DispatchQueue 中使用弱引用。
【解决方案2】:

这里是 UIButton 扩展,带有动画的短时间颜色更改背景。

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

【讨论】:

    【解决方案3】:

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

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

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

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

    【讨论】:

    • 这是一个更好的解决方案。
    【解决方案4】:

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

    只需将您的“updateUI ()”函数更改如下:

    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 秒(您可以根据自己的喜好更改此数字)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-10
      • 2015-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-19
      • 1970-01-01
      • 2020-07-17
      相关资源
      最近更新 更多