添加功能到计时器

时间:2015-09-04 19:22:07

标签: ios swift function timer xcode6

如果我想在计时器命中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)"
    }
}

2 个答案:

答案 0 :(得分:0)

根据您的计时器调用延迟功能,

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

引用的

答案 1 :(得分:0)

试试这个:

   var countDown = 0 

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