【问题标题】:How to change font color/properties with a countdown timer?如何使用倒数计时器更改字体颜色/属性?
【发布时间】:2017-07-30 02:49:52
【问题描述】:

我正在开发一个测验应用程序,用户有 15 秒的时间来回答问题,然后才会认为问题是错误的。倒数计时器的文本设置为白色,因为它实际上只是一个标签。有没有办法在代码中将其更改为当倒数计时器达到 5 秒时字体变为红色以提醒用户时间快到的标志?

这是我的相关代码:

导入 UIKit

 class ViewController: UIViewController {


func updateCounter() {




    counter -= 1
    questionTimer.text = String(counter)

    if counter == 0 {


        timer.invalidate()
        wrongSeg()
        }


}

  //variables
var counter = 15

var timer = Timer()

@IBOutlet weak var questionTimer: UILabel!

【问题讨论】:

    标签: ios fonts timer switch-statement alert


    【解决方案1】:

    你可以使用

     var counter: Int = 15 {
        didSet {
            print("The value of counter changed from \(oldValue) to \(counter)")
            if (counter == 5){
            //Change the question timer color.
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用此代码:

      func updateCounter() {
          counter -= 1
          if counter <= 5{
              questionTimer.textColor = .red;
          }
          questionTimer.text = String(counter)
      
          if counter == 0 {
      
      
              timer.invalidate()
              wrongSeg()
              }
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 2013-09-27
        • 1970-01-01
        • 2011-10-03
        • 2011-02-24
        • 1970-01-01
        • 1970-01-01
        • 2019-08-13
        • 2015-05-18
        • 2011-05-20
        相关资源
        最近更新 更多