iOS swift-倒计时

var daojishiTimer:Timer!

var daojishi = 4

func daoJiShi() -> () {

// 启用计时器,控制每秒执行一次tickDown方法

daojishiTimer = Timer.scheduledTimer(timeInterval: 1,

target:self,selector:#selector(tickDown),

userInfo:nil,repeats:true)

}

/**

*计时器每秒触发事件

**/

func tickDown()

{

daojishi -= 1

if daojishi == 0 {

self.linshiSaoBtn.setTitleColor(btnBgColor, for: .normal)

self.linshiSaoBtn.setTitle("开锁", for: .normal)

self.kaiSuoImg.image = UIImage.init(named:"lALO2gyzUSQk")

self.kaisuoBeiJinBtn.isEnabled = true

self.linshiSaoBtn.isEnabled = true

//停止定时器

daojishiTimer.invalidate()

}else{

self.linshiSaoBtn.setTitleColor(UIColor.gray, for: .normal)

self.linshiSaoBtn.setTitle("\(daojishi)", for: .normal)

self.kaiSuoImg.image = UIImage.init(named:"lALO2gyzUSQk")

}

}

//参考:http://blog.csdn.net/qq_29846663/article/details/54667961

你可能感兴趣的:(iOS swift-倒计时)