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 ...
随机推荐
- 关于安卓苹果手机安装证书抓https的关键步骤
苹果有关键步骤!!!
- [UE4]场景光照改进PostProcessVolume
PostProcessVolume可以做的效果很多,其中就可以实现太阳光斑效果. Unbound勾上上,就表示不受“PostProcessVolume”组件的大小限制,直接应用到整个世界.如果不勾选, ...
- 如何在一个js文件中引入另外的js文件
例如想要在a.js中引用b.js.c.js和d.js document.write("<script language='javascript' src='b.js'></ ...
- Ubuntu 提权漏洞(CVE-2019-7304)复现
漏洞描述: Ubuntu 版本: Ubuntu 18.10 Ubuntu 18.04 LTS Ubuntu 16.04 LTS Ubuntu 14.04 LTS 2.28 < snapd < ...
- 最近学习下,nohup和&的区别
nohup是永久执行 &是指在后台运行 运行 nohup --helpRun COMMAND, ignoring hangup signals. 可以看到是“运行命令,忽略挂起信号” 就是指, ...
- redhat 7安装CentOS 7 yum源
http://www.bubuko.com/infodetail-2004218.html http://www.bubuko.com/infodetail-2004218.html ******** ...
- 28. 表单css样式定义格式
form>table>tbody>tr>td{padding:5px;font-size:14px;font-family:"Microsoft YaHei" ...
- springmvc @valid
JSR303是javaEE6中的一个子规范:Bean Validation.官方实现是HibernateValidatior.校验: 使用springmvc 的validate机制,需要引入valid ...
- Redis 配置节
Redis 后面的配置基于4.0.9版本=>>>不指定版本信息的配置说明都是耍流氓 比如在4.0.9中没有vm相关的及glueoutputbuf的配置信息 部分常用配置节(后面有详细 ...
- 16 MySQL--正确使用索引
count 统计 count(*)和count(字段名) 基本结果是一样的 但是一种情况例外,就是当某字段名下边的数据有null值的时候,不计入这个count中,*则全部列入count中 一 .索引未 ...