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. 池以及barrier简单

    用了下CyclicBarrier,注意线程池中的线程数量设置,还有就是DB连接的时候,需要考虑单个DB能承受的最大连接数目和每个连接上能同时打开的cursor等限制,需要时可以通过jstack查看堆栈 ...

  2. 关于STRUCT优化的一个点

    在西山居的这篇U3D cheatsheet中,提到: c12. 确保 struct 实现了 Equals() 和 GetHashCode() 这怎么理解? 首先,看下system.object.equ ...

  3. Zabbix点滴记录

    转自 眄眄的闺蜜 简单检查中的icmppingloss[<target>,<packets>,<interval>,<size>,<timeout ...

  4. js 监听浏览器刷新还是关闭事件 - 转

    监听页面关闭: window.onbeforeunload = function() { //鼠标相对于用户屏幕的水平位置 - 窗口左上角相对于屏幕左上角的水平位置 = 鼠标在当前窗口上的水平位置 v ...

  5. 【原】Ubuntu virtual terminal

    CTRL+ALT+F1 ~ F6 six virtual terminal ALT-F7 return to graphic desktop

  6. c#二维码资料

    几个主要的C#二维码开发资料,用于学生的本科毕业论文参考 https://blog.csdn.net/xwnxwn/article/details/72636417 C# ZXing.Net生成二维码 ...

  7. EF 数据查询效率对比

    优化的地方: 原地址:https://www.cnblogs.com/yaopengfei/p/9226328.html ①:如果仅是查询数据,并不对数据进行增.删.改操作,查询数据的时候可以取消状态 ...

  8. 历届试题 大臣的旅费-(树的直径+dfs)

    问题描述 很久以前,T王国空前繁荣.为了更好地管理国家,王国修建了大量的快速路,用于连接首都和王国内的各大城市. 为节省经费,T国的大臣们经过思考,制定了一套优秀的修建方案,使得任何一个大城市都能从首 ...

  9. 源码编译安装Python3及问题解决

    https://chowyi.com/%E6%BA%90%E7%A0%81%E7%BC%96%E8%AF%91%E5%AE%89%E8%A3%85Python3%E5%8F%8A%E9%97%AE%E ...

  10. 简单的实现微信获取openid

    微信公众平台获取openid在公众号的开发中有很多用途,前段时间为实现用户使用公众号在登录一次以后可以免密登陆而使用了openid.开发过程中遇到了一些问题,在这里向需要且还没有获取到openid的米 ...