简体   繁体   English

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

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

I'm trying to create a Quiz app that consists of two buttons, false and true button.我正在尝试创建一个由两个按钮(假按钮和真按钮)组成的测验应用程序。 My question is, when I press one of the two buttons, I want it to change its background colour ONLY SHORTLY when its pressed then I want it to go back to the color it originally was, but I can not figure out how to change the background color shortly.我的问题是,当我按下两个按钮之一时,我希望它在按下时仅在短时间内更改其背景颜色,然后我希望它 go 恢复到原来的颜色,但我不知道如何更改背景颜色很快。 This is the code I have for this part so far:到目前为止,这是我对这部分的代码:

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

You can try你可以试试

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

to use it使用它

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

And change并改变

updateUI()

to

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

Here is the UIButton extension with the change background for short time color with animation.这是 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
        }
    }
}

Use:采用:

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

This will help you to change the button colour to colour you want (green or red or whatever) and change it back to clear with delay of 0.2 seconds.这将帮助您将按钮颜色更改为您想要的颜色(绿色或红色或其他颜色)并延迟 0.2 秒将其更改回清除。 In the easiest way possible.以最简单的方式。

In the IBAction of answerButtonPressed put the following lines after the answer check is done.在答案检查完成后,在 answerButtonPressed 的 IBAction 中放置以下行。

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

In selector, specify your function used to update the user interface.在选择器中,指定用于更新用户界面的 function。

I'm trying to create a Quiz app that consists of two buttons, false and true button.我正在尝试创建一个包含两个按钮的测验应用程序,即 false 和 true 按钮。 My question is, when I press one of the two buttons, I want it to change its background colour ONLY SHORTLY when its pressed then I want it to go back to the color it originally was, but I can not figure out how to change the background color shortly.我的问题是,当我按下两个按钮之一时,我希望它仅在按下它的时候改变它的背景颜色,然后我希望它 go 回到原来的颜色,但我不知道如何改变背景颜色很快。 This is the code I have for this part so far:这是我到目前为止这部分的代码:

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

In this situation, the following would be the fastest and simplest solution:在这种情况下,以下将是最快和最简单的解决方案:

Just change your 'updateUI ()' function as follows:只需更改您的 '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
        }
        
    }

So what's happening here?那么这里发生了什么?

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

The above line of code is holding off green/red color for 0.3 secs (you can change this number to your preference) before changing the background back to clear color.在将背景更改回清晰颜色之前,上面的代码行将绿色/红色保持 0.3 秒(您可以根据自己的喜好更改此数字)。

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

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