为定时器添加功能

Add function to timer

如果我想在计时器到达 0 时尽快添加功能。 有人有什么建议吗?这是我想在其中实现它的代码。

class ViewController: UIViewController {
    @IBOutlet weak var Counterlabel: UILabel!
    var Nbr = 51
    var timerValue = 52
    var timer = NSTimer()

    override func viewDidLoad() {
        super.viewDidLoad()
        let timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("Counter"), userInfo: nil, repeats: true)
    }

    @IBAction func Btn1(sender: AnyObject) {
        Nbr = timerValue - 1
        timerValue = timerValue - 1;
    }

    func Counter(){
        Nbr -= 1
        Counterlabel.text = "\(Nbr)"
    }
}

像这样根据你的计时器调用延迟函数,

func delay(delay:Double, closure:()->()) {
     dispatch_after(
          dispatch_time(
               DISPATCH_TIME_NOW,
               Int64(delay * Double(NSEC_PER_SEC))
          ),
     dispatch_get_main_queue(), closure)
}

delay(Double(self.Nbr)) {
    //Do your work here
}

以上函数引用自here

试试这个:

   var countDown = 0 

    func Counter()
  {
     countDown--;
     if countDown == 0 
    {
       // reset the timer     
    }
  }