国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Swift > 正文

Swift3.0 GCD定時器的使用DEMO

2020-03-09 17:46:38
字體:
來源:轉載
供稿:網友

 直接看主要代碼

//截止日期let endDate = datePicker.date//開始日期let startDate = Date()//時間間隔let timeInterval:TimeInterval = endDate.timeIntervalSince(startDate)if timer == nil { //剩余時間 var timeout = timeInterval if timeout != 0 { //創建全局隊列 let queue = DispatchQueue.global() //在全局隊列下創建一個時間源 timer = DispatchSource.makeTimerSource(flags: [], queue: queue) //設定循環的間隔是一秒,并且立即開始 timer?.scheduleRepeating(wallDeadline: DispatchWallTime.now(), interval: .seconds(1)) //時間源出發事件 timer?.setEventHandler(handler: {  //必須是當前日期往后的日期,在datePicker上也做了限制  if timeout <= 0 {  self.timer?.cancel()  self.timer = nil  DispatchQueue.main.async(execute: {   self.day.text = "00"   self.hour.text = "00"   self.minute.text = "00"   self.second.text = "00"  })  } else {  //計算剩余時間  let days = Int(timeout) / (3600 * 24)  if days == 0 {   self.day.text = ""  }  let hours = (Int(timeout) - Int(days) * 24 * 3600) / 3600  let minutes = (Int(timeout) - Int(days) * 24 * 3600 - Int(hours) * 3600) / 60  let seconds = Int(timeout) - Int(days) * 24 * 3600 - Int(hours) * 3600 - Int(minutes) * 60  //主隊列中刷新UI  DispatchQueue.main.async(execute: {   if days == 0 {   self.day.text = "0"   } else {   self.day.text = "/(days)"   }   if hours < 10 {   self.hour.text = "0" + "/(hours)"   } else {   self.hour.text = "/(hours)"   }   if minutes < 10 {   self.minute.text = "0" + "/(minutes)"   } else {   self.minute.text = "/(minutes)"   }   if seconds < 10 {   self.second.text = "0" + "/(seconds)"   } else {   self.second.text = "/(seconds)"   }  })  timeout -= 1  } }) //啟動時間源 timer?.resume() }}

DEMO效果圖

swift3.0,倒計時,swift,定時器倒計時

DEMO下載地址


注:相關教程知識閱讀請移步到swift教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 定结县| 涞水县| 本溪| 宾川县| 沧州市| 锦州市| 名山县| 上杭县| 吴川市| 宿松县| 韶关市| 广德县| 佳木斯市| 静安区| 泗洪县| 海口市| 翁源县| 崇义县| 察隅县| 德江县| 开鲁县| 仙桃市| 望都县| 齐河县| 汉沽区| 南靖县| 杂多县| 广平县| 明光市| 惠安县| 洛阳市| 景德镇市| 隆子县| 宿松县| 黄平县| 莲花县| 离岛区| 荥经县| 张北县| 塘沽区| 哈巴河县|