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. zookeeper windows7下集群搭建

    模拟分布式环境!!!  搞了好几天,各种错误!!终于成功了. 环境: windows7 /centos/xsheel    安装了三个虚拟机... 1.下载zookeeper http://archi ...

  2. MySQL更新优化(转)

    通常情况下,当访问某张表的时候,读取者首先必须获取该表的锁,如果有写入操作到达,那么写入者一直等待读取者完成操作(查询开始之后就不能中断,因此允许读取者完成操作).当读取者完成对表的操作的时候,锁就会 ...

  3. ThinkPHP5分页样式设置

    手册上讲分页类的使用时对样式讲的不够详细,这里我结合个人的摸索给大家一些参考意见. config里的分页配置我使用的是系统默认的bootstrap,查看thinkphp\library\think\p ...

  4. Grafana+Zabbix使用配置

    官方提供的网友分享的图形面板,可以自行选择使用下载---  https://grafana.com/dashboards   Grafana 是 Graphite 和 InfluxDB 仪表盘和图形编 ...

  5. VMware Harbor学习

    同时安装Clair和Notary# ./install.sh --with-notary --with-clair 与notary或者Clair一起安装时管理Harbor的生命周期当Harbour与N ...

  6. Ajax核心技术代码

    /* @author weichen */ var xhr = ''; function Ajax() { if(window.XMLHttpRequest) { var xhr = new XMLH ...

  7. Delphi实现悬浮的卡拉OK字幕

    千千静音的悬浮式卡拉OK字幕,大家一定觉得不错吧,其实用Delphi,你也可以很容易的做得到.首先我们在Delphi中新建一个700*130左右的窗体,上面放置两个Image控件,Image控件的Al ...

  8. springboot 整合task定时任务

    一步:在启动类中加入     加入就会调用定时了. //开启定时任务 开启后就可以被扫描到   @EnableScheduling 二步:建一个tasks工具包 都会被扫描到的了 有三个类 Async ...

  9. C# 保证数据长度相同

    /// <summary> /// 保证数据长度相同 /// </summary> /// <param name="obj"></par ...

  10. 《深入理解JAVA虚拟机》----------第三章 垃圾收集器与内存分配策略,笔记(下)

    1.垃圾收集器 1.1 Serial收集器 这个收集器是一个单线程的收集器,它在进行垃圾收集时,必须暂停其他所有的工作线程. 它是虚拟机运行在Client模式下的默认新生代收集器,它简单而高效. 1. ...