swift 定时器

/ Mac / 没有评论 / 2283浏览

swift 定时器

定时器的两种简单实现方式:

1.timer

var second = 60
var timer : Timer?

// 1.点击按钮开始计时
button.addTarget(self, action: #selector(startTimer) , for: .touchUpInside)

// 2.开始计时
func startTimer() {

    timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updataSecond), userInfo: nil, repeats: true)
    //调用fire()会立即启动计时器
    timer!.fire()
 }

 // 3.定时操作
 @objc func updataSecond() {
     if second>1 {
        //.........
        second -= 1
     }else {
        stopTimer()
     }
 }

// 4.停止计时
func stopTimer() {
    if timer != nil {
         timer!.invalidate() //销毁timer
         timer = nil
     }
 }

2.GCD

func startTimer() {
     // 定义需要计时的时间
     var timeCount = 60
     // 在global线程里创建一个时间源
     let timer = DispatchSource.makeTimerSource(queue: DispatchQueue.global())
     // 设定这个时间源是每秒循环一次,立即开始
     timer.schedule(deadline: .now(), repeating: .seconds(1))
     // 设定时间源的触发事件
     timer.setEventHandler(handler: {
         // 每秒计时一次
         timeCount = timeCount - 1
         // 时间到了取消时间源
         if timeCount <= 0 {
             timer.cancel()
         }
         // 返回主线程处理一些事件,更新UI等等
         DispatchQueue.main.async {
             print("-------%d",timeCount);
         }
     })
     // 启动时间源
     timer.resume()
  }

上面两种方法以及实现简单的定时器,但是还有一个隐藏的问题,那就是当应用切换至后台时,定时任务就会中断。如果我们想应用在后台也能正常进行定时,就需要在AppDelegate.swift做些处理。

// 后台任务标识
var backgroundTask:UIBackgroundTaskIdentifier! = nil

func applicationDidEnterBackground(_ application: UIApplication) {
        
     // 延迟程序静止的时间
     DispatchQueue.global().async() {
         //如果已存在后台任务,先将其设为完成
         if self.backgroundTask != nil {
             application.endBackgroundTask(self.backgroundTask)
             self.backgroundTask = UIBackgroundTaskInvalid
         }
     }
        
     //如果要后台运行
     self.backgroundTask = application.beginBackgroundTask(expirationHandler: {
            () -> Void in
         //如果没有调用endBackgroundTask,时间耗尽时应用程序将被终止
         application.endBackgroundTask(self.backgroundTask)
         self.backgroundTask = UIBackgroundTaskInvalid
     })
 }

更详细的解读可以参考iOS后台任务,或者是官方文档