swift - 封装 GCDTimer 和 NSTimer
封装的类代码
import 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")
}
// /// 设置定时器
// func initilAndStartTimer(timeInterval: TimeInterval,handleBlock:@escaping (() -> Void)) {
// self.timer = Timer.scheduledTimer(withTimeInterval: timeInterval, repeats: true, block: { t in
// handleBlock()
// })
// }
// /// 暂停或者重启定时器定时器
// func stopOrStartTimer(isStop: Bool) {
// self.timer?.fireDate = isStop == true ? Date.distantFuture : Date.distantPast
// }
// /// 销毁定时器
// func invaliTimer() {
// self.timer?.invalidate()
// self.timer = nil
// }
}
/// GCD定时器相关方法
extension ZDTimerTool{
/// 初始化得到GCD定时器
func DispatchTimer(delayTime: Double = , 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()
}
}
}else{
self.GCDTimer?.setEventHandler{
DispatchQueue.main.async {
handleBlock()
}
}
}
self.GCDTimer?.schedule(deadline: DispatchTime.now(), repeating: timeInterval)
// self.GCDTimer?.schedule(wallDeadline: DispatchWallTime.now(), repeating: timeInterval)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + delayTime) { [weak self] in
self?.GCDTimer?.resume()
} }
/// 暂停或者重启GCDTimer
func stopOrResumeGCDTimer(isStop: Bool){
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
}
}
使用方法:
属性 timer 和时间
//倒计时
var countdownTimer = ZDTimerTool() // remainingSeconds代表当前倒计时剩余的秒数
var remainingSeconds: Int = //在需要的地方开启倒计时
countdownTimer.DispatchTimer(timeInterval: ) { [weak self] in
self?.handTimer()
} func handTimer() {
self.remainingSeconds -=
if self.remainingSeconds == {//倒计时0秒
self.remainingSeconds =
self.sendButton.setTitle("重新发送", for: .normal)
self.sendButton.backgroundColor = UIColor.red
self.sendButton.isEnabled = true
self.countdownTimer.stopOrResumeGCDTimer(isStop: true)
}else{
sendButton.setTitle("\(remainingSeconds)秒后重新获取", for: .normal)
self.sendButton.backgroundColor = UIColor.gray
sendButton.isEnabled = false
}
}
swift - 封装 GCDTimer 和 NSTimer的更多相关文章
- 初识 swift 封装轮播图
一.简介 换了一家公司.换了一个环境刚开始来公司自然不能有一丝一毫的放松,每天即使是没有什么工作也是看看这个博客.那个源码.尽量让自己更充实.慢慢的开始写几篇博客记录下自己遇到的一些问题和解决方法.其 ...
- 纯 Swift 封装的 SQLite 框架:SQLite.swift
SQLite.swift 是一个使用纯 Swift 语言封装 SQLite3 的操作框架. 特性: 简单的查询和参数绑定接口 安全.自动类型数据访问 隐式提交和回滚接口 开发者友好的错误处理和调试 文 ...
- Swift 封装
前言 封装主要有两大目的:一是为了我们使用数据更加方便,二是为了数据保护. 1.Swift 访问修饰符 在 Swift 语言中,访问修饰符也分为三类,分别是 private.internal.publ ...
- swift - 封装百度地图
1. #import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件 #import <BaiduMapAPI_Map/B ...
- swift版的GCD封装
swift版的GCD封装 说明 本人针对swift封装了GCD,包括GCDQueue,GCDGroup,GCDTimer以及GCDSemaphore,使用较为便利. 源码 https://github ...
- Swift 2.0 封装图片折叠效果
文/猫爪(简书作者)原文链接:http://www.jianshu.com/p/688c491580e3著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 用Swift封装图片折叠效果 b ...
- swift 自定义图片轮播视图
Swift封装图片轮播视图: import UIKit class XHAdLoopView: UIView { private var pageControl : UIPageControl? pr ...
- Swift和OC相互调
在项目中不免会有多中语言开发, 不说别的就我个人而言, 之前一直都是用 OC 写的代码, 封装很多工具类, 而苹果新出来 Swift , 现在项目在向 Swift 过渡, 或者新项目是 Swift , ...
- Swift & OC 混编 浅析
转载自:http://www.infoq.com/cn/articles/wangyi-cartoon-swift-mixed-practice?utm_campaign=rightbar_v2&am ...
随机推荐
- pyH支持python3
记录下,感谢大神,原地址https://www.cnblogs.com/yunmenzhe/p/6293428.html,侵删 1.修改xxx/python3.5/pyh.py权限 sudo chmo ...
- 2015ACM-ICPC长春E题(hdu5531)题解
一.题意 No response.T_T 二.思路 分$n$为奇数或者偶数讨论. 如果$n$是奇数,列出不等式组:$r_1+r_2=d_{1},r_2+r_3=d_{2},r_3+r_4=d_{3}, ...
- hint之qb_name
http://www.thinkindata.com/?p=34 该hint用于子查询(query_block) 很多的情况下,如果子查询共用相同的别名(alias), 可以通过设定不同的qb_n ...
- 运维不得不知的 Linux 性能监控、测试、优化工具
Linux 平台上的性能工具有很多,眼花缭乱,长期的摸索和经验发现最好用的还是那些久经考验的.简单的小工具.系统性能专家 Brendan D. Gregg 在 LinuxCon NA 2014 大会上 ...
- sql server查看表占用索引空间(小技巧)
选择表右键—属性—存储—索引空间
- python学习之RabbitMQ-----消息队列
RabbitMQ队列 首先我们在讲rabbitMQ之前我们要说一下python里的queue:二者干的事情是一样的,都是队列,用于传递消息 在python的queue中有两个一个是线程queue,一个 ...
- Spring MVC 学习笔记3 - 利用Default Annotation 模式获取请求,使Controller与View对应,并传值。
1. WEB-INF/web.xml 这里定义了获取请求后,执行的第一步.抓取请求. <servlet> <servlet-name>appServlet</servle ...
- OpenCL 图像卷积 3 使用 CPU
▶ CPU 图像卷积,共四种方法.分别为基本串行,使用模板,使用局部内存,使用AVX指令优化 ● 全部的代码,仅在主函数中选择调用的函数名即可. #include <stdio.h> #i ...
- spring data jpa 原生查询(查一个json中的某一字段)
数据库: jpa 查询 java: @Component("ediHistoryDAO") public interface EdiHistoryDAO extends CrudR ...
- JDK9-模块化系统
Content 0. 实例 0.1 使用命令行编写和运行模块程序 0.1.1 设置目录 0.1.2 编写源代码 0.1.3 编译 0.1.4 打包模块代码 0.1.5 运行程序 0.2 使用eclip ...