swift 定时器的启用

/ Mac / 没有评论 / 1610浏览

swift 定时器的启用

我们有时启用一个定时器来做一些事情,比如计时等。启用一个计数,60秒,停止

var timer = Timer()
var count =  0

func start(){
  timer = Timer.scheduledTimer(timeInterval:1, target: self,selector:#selector(ViewController.changevalue),userInfo:nil,repeats: true)
}

//这里的 viewcontroller可以用self代替。 Changevalue是你循环的函数名
func changevalue() {
  //代码
  count += 1
  if count  > =60
  timer.invalidate()
}