代码之家  ›  专栏  ›  技术社区  ›  Box House

Swift 4:如何在我的UIbutton中添加倒计时计时器。片名?

  •  3
  • Box House  · 技术社区  · 7 年前

    我正在尝试在中的ui按钮内添加一个倒计时计时器。设置标题。我该怎么做?下图是我正在努力实现的目标。

    enter image description here

    enter image description here

    然后,一旦计时器设置为0,我希望背景颜色更改为不同的颜色。

    这是迄今为止我掌握的代码。

    let sendSMSAgainNumberOfTime: UIButton = {
        let button = UIButton(type: .system)
        button.backgroundColor = .black
        button.layer.cornerRadius = 7
        button.setTitle("SEND SMS AGAIN IN 36", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = UIFont(name: "open sans", size: 16)
        return button
    }()
    
    2 回复  |  直到 7 年前
        1
  •  10
  •   Shehata Gamal    7 年前

    你可以试试计时器

     var countTimer:Timer!
    
     var counter = 36
    

    //

    在里面 viewDidLoad 设置它

     self.countTimer = Timer.scheduledTimer(timeInterval: 1 ,
                                                  target: self,
                                                  selector: #selector(self.changeTitle),
                                                  userInfo: nil,
                                                  repeats: true)
    
    func changeTitle()
    {
         if counter != 0
         {
             button.setTitle("SEND SMS AGAIN IN \(counter)", for: .normal)
             counter -= 1
         }
         else
         {
              countTimer.invalidate()
    
              button.backgroundColor = // set any color
         }
    
    }
    

    //

    或使用IOS 10+计时器块

    //

    let sendSMSAgainNumberOfTime: UIButton = {
        let button = UIButton(type: .system)
        button.backgroundColor = .black
        button.layer.cornerRadius = 7
        button.setTitle("SEND SMS AGAIN IN 36", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = UIFont(name: "open sans", size: 16)
    
        // timer block exists from ios 10 +
    
        if #available(iOS 10.0, *) {
    
            Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { (t) in
    
                if(button.tag != 37)
                {
                    button.setTitle("SEND SMS AGAIN IN \(36-button.tag)", for: .normal)
    
                    button.tag += 1
                }
                else
                {
                    t.invalidate()
    
                    button.backgroundColor = // set any color
                }
    
    
            })
        } else {
            // Fallback on earlier versions
        }
    
        return button
    }()
    
        2
  •  7
  •   Arin    5 年前

    您也可以在Swift 5中执行以下操作。这是一个非常简单的倒计时,点击按钮后会打印出剩余的每一秒。如果您有任何问题,请告诉我(:

    class ViewController: UIViewController {
    
        var secondsRemaining = 30
    
        @IBAction func startTimer(_ sender: UIButton) {
    
            Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (Timer) in
                if self.secondsRemaining > 0 {
                    print ("\(self.secondsRemaining) seconds")
                    self.secondsRemaining -= 1
                } else {
                    self.invalidate()
                }
            }
    
        }