mport UIKit

/// 控制定时器的类
class ZDTimerTool: NSObject {
/// 定时器
// private var timer: Timer?
/// GCD定时器
private var GCDTimer: DispatchSourceTimer?
/// GCD定时器的挂起状态
private var isSuspend: Bool = false
override init() {
super.init()
}
deinit {
// 对象在销毁前会销毁定时器,所以使用定时器应该设置全局的属性
// self.invaliTimer()
self.invaliGCDTimer()
DDLOG(message: "deinit: ZDTimerTool")
}
}
/// GCD定时器相关方法
extension ZDTimerTool{
/// 初始化得到GCD定时器
func DispatchTimer(timeInterval: TimeInterval , handleBlock:@escaping (() -> Void)) {
if self.GCDTimer == nil {
self.GCDTimer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.main)
self.GCDTimer?.schedule(deadline: DispatchTime.now(), repeating: timeInterval)
self.GCDTimer?.setEventHandler{
DispatchQueue.main.async {
handleBlock()
}
}
self.GCDTimer?.resume()
self.GCDTimer?.schedule(deadline: DispatchTime.now(), repeating: timeInterval)
}else{
self.stopOrResumeGCDTimer(isStop: false)
} }
/// 暂停或者重启GCDTimer
func stopOrResumeGCDTimer(isStop: Bool){
guard self.isSuspend != isStop else {
return
}
isStop == true ? self.GCDTimer?.suspend() : self.GCDTimer?.resume()
self.isSuspend = isStop
}
/// 销毁GCD定时器
func invaliGCDTimer() {
if self.isSuspend == true {
self.GCDTimer?.resume()
}
self.GCDTimer?.cancel() //销毁前不能为suspend(挂起状态)
self.GCDTimer = nil
}
}

  

2.使用

    //倒计时
var countdownTimer = ZDTimerTool()

  

适当地方开启定时器
countdownTimer.DispatchTimer(timeInterval: 1) { [weak self] in
self?.handTimer()
}

  

    func handTimer() {
if self.remainingSeconds == 0{
self.remainingSeconds = 60
self.sendButton.setTitle("重新发送", for: .normal)
self.sendButton.backgroundColor = UIColor.init(hexColor: "FF8E00")
self.sendButton.isEnabled = true
self.countdownTimer.stopOrResumeGCDTimer(isStop: true) }else{
sendButton.setTitle("\(remainingSeconds)秒后重新获取", for: .normal)
self.sendButton.backgroundColor = UIColor.gray
sendButton.isEnabled = false
}
self.remainingSeconds -= 1
}

  

swift - 添加定时器的更多相关文章

  1. Swift - 添加纯净的Alamofire

    Swift - 添加纯净的Alamofire 如果你有代码洁癖,不能容忍任何多余的东西,请继续往下看.  . 下载Alamofire (https://github.com/Alamofire/Ala ...

  2. 关于给springboot添加定时器的两种方式

    原文:https://blog.csdn.net/liboyang71/article/details/72781526 首先,搭建好一个springboot项目,可使用maven或者gradle或者 ...

  3. Atitit.aticmd v4  新特性q39 添加定时器释放功能

    Atitit.aticmd v4  新特性q39 添加定时器释放功能 V1  实现兰cmd V2 标准输入,标准输出,标准错误与重新定向 V3  stdout stderr统一重新定向 V4  添加定 ...

  4. IOS 添加定时器(NSTimer)

    定时器 CADisplayLink:时间间隔比较小(使用时间频率高)的时候用(适合小游戏中开发) NSTimer:时间间隔比较大的时候调用(适合图片轮放的时候用) //声明定时器 @property ...

  5. swift多线程定时器

    swift多线程定时器的使用 func countDown(_ timeOut:Int,view: UIView){ var timeout = timeOut let queue:DispatchQ ...

  6. 在Windows服务中添加定时器

    创建windows服务: 在VisualStudio中用C#创建一个Windows服务,微软MSDN参考地址: http://msdn.microsoft.com/zh-cn/library/zt39 ...

  7. iOS - Swift NSTimer 定时器

    前言 public class NSTimer : NSObject 作用 在指定的时间执行指定的任务. 每隔一段时间执行指定的任务. 1.定时器的创建 当定时器创建完(不用 scheduled 的, ...

  8. spring项目中如何添加定时器以及在定时器中自动生成sprng注入对象

    最近做了一个java的项目,部门领导给了一套代码让我尽快掌握,说心里话本人真心不喜欢java的这种项目方式,各种配置各种xml文件简直头都大了,下面就将我遇到的其中一个我认为是坑的地方整理出来,希望能 ...

  9. Swift - 添加、修改、删除通讯录联系人

    使用AddressBook.framework框架,我们除了可以很方便的获取通信录里的联系人.同时,还能对通讯录进行新增.修改.删除联系人操作. (注意:这些操作同查询一样,首先需要发起授权请求) 1 ...

随机推荐

  1. EF 配置MySQL

    添加 mysql dll 引用 WebConfig 配置: 1.先添加connectionstrings,providerName 换成 mysql的 <connectionStrings> ...

  2. native.js 判断是否安装某app

    例:是否安装微信 function isWeixin() { var UIApplication = plus.ios.importClass("UIApplication"); ...

  3. 虚拟机mac 与主机的网络共享

    1. 主机建立共享文件夹 aaa 2.虚拟机采用桥接 3.mac打开Finder 找到 “前往” 连接服务器”输入“smb://192.168.1.xx”(你电脑的ip地址),点击连接.

  4. 尚硅谷springboot学习17-SpringBoot日志

    SpringBoot使用它来做日志功能: <dependency> <groupId>org.springframework.boot</groupId> < ...

  5. Lazarus中Windows单元问题

    在程序中加入Windows 单元后,经常会使一些过程和函数莫名其妙的报错,这是因为Windows单元很多函数,过程 与sysutils 单元重名 ,所以一般要把windows 引用放在 sysutil ...

  6. img标签在div里上下居中

    方法一:图片尺寸未知,IE8-不支持 CSS部分: <style> .content{ width:500px; height:500px; border:1px solid black; ...

  7. How to Pronounce the Letters NG – No Hard G

    How to Pronounce the Letters NG – No Hard G Share Tweet Share Most of the time when you see the lett ...

  8. adb常用命令及详解

    ADB 即 Android Debug Bridge,Android调试桥.ADB工作方式比较特殊,采用监听Socket TCP 端口的方式让IDE和Qemu通讯,默认情况下adb会daemon相关的 ...

  9. 16.0 Auth0注册与设置

    首先呢?注册https://manage.auth0.com 填写回调网页,意思是当我们点sign in 那个按钮的时候 会访问这个官网 这个官网又回调下面的网页,不然会报错.这个网站因为我们是开发所 ...

  10. servlet(2)response常用方法

    详细的response 学习笔记是: 输出到前台的的方法 1:使用OutputStream流向客户端浏览器输出中文数据 2:使用PrintWriter流向客户端浏览器输出中文数据 3:使用Output ...