iOS14开发-Timer
2023-03-15 23:27:40 时间
定时器可以设置按固定周期执行某个操作。iOS 中主要有 3 种定时器,本文先讲解第一种 Timer。
使用
- iOS 10 之后下面的方法不会出现循环引用的问题,推荐使用。
// 自动执行的Timer
// 参数一:时间间隔,参数二:是否重复执行
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (timer) in
// 定期执行的代码
print("Hello World")
}
// 需要手动开启的Timer
let timer = Timer(timeInterval: 2.0, repeats: true) { (timer) in
// 定期执行的代码
print("Hello World")
}
RunLoop
- 用来处理事件的循环,保持程序持续运行,App 启动后会开启一个主线程,主线程启动时会运行一个对应的 RunLoop,RunLoop 保证主线程不会被销毁,从而保证了程序的持续运行。
- RunLoop 工作模式有 5 种,常见模式有以下 3 种:
-
default
:默认模式。 -
tracking
:界面跟踪模式。 -
common
:通用模式,前面 2 种的结合。
-
- 将 Timer 添加到 RunLoop 后会自动开始工作。
// 界面发生拖拽就会停止执行
RunLoop.current.add(timer, forMode: .default)
// 界面拖拽才会执行
RunLoop.current.add(timer, forMode: .tracking)
// 上面两种模式的结合,常用
RunLoop.current.add(timer, forMode: .common)
暂停与重启
// 暂停
timer.fireDate = Date.distantFuture
// 重启
timer.fireDate = Date.distantPast
销毁
invalidate:阻止 Timer 再次触发并请求将其从 RunLoop 中删除,因此这种方式停止后无法让其重新开始工作。
timer.invalidate()
案例:倒计时
import UIKit
class ViewController: UIViewController {
// 倒计时总时长(秒)
var count = 5
// 定时器
var timer: Timer!
// UILabel+UITapGestureRecognizer比UIButton好
lazy var label: UILabel = {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
label.center = view.center
label.text = "获取倒计时"
label.textAlignment = .center
label.isUserInteractionEnabled = true
label.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleCount)))
return label
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(label)
}
@objc func handleCount() {
// 倒计时过程中不允许点击
label.isUserInteractionEnabled = false
// 创建Timer,每隔1s执行一次
timer = Timer(timeInterval: 1.0, repeats: true) { _ in
// 显示倒计时
self.label.text = "(self.count)"
// 计时减1
self.count -= 1
// 当计时为0时
if self.count == 0 {
// 停止计时
self.timer.invalidate()
// 改变文字
self.label.text = "重新获取"
// 允许交互
self.label.isUserInteractionEnabled = true
// 恢复倒计时总时长
self.count = 5
}
}
// 添加RunLoop,定时器开始工作
RunLoop.current.add(timer, forMode: .common)
}
}
相关文章
- Python中的函数与方法 以及Bound Method和Unbound Method
- 一文贯通python文件读取
- Python 中的异步编程:Asyncio
- 7个你现在就该学习Python的理由
- 提高Python运行效率的六个窍门
- Python数据科学:神经网络
- 一篇文章看懂大数据分析就业前景及职能定位
- R和Python中的文本挖掘:8个入门小贴士
- 告诉你为什么Python有点慢,但我却无所谓?
- 专注学习DevOps编程语言Top 5推荐
- Python发送邮件脚本
- Python多进程并行编程实践: mpi4py 的使用
- Python语言在未来的发展前景
- Python vs Ruby: 谁是最好的 web 开发语言?
- Python对Ruby:谁在Web开发领域更胜一筹?
- Python一行代码完成并行任务
- Python开发者2017应该关注的七个类库
- python爬虫入门基本知识
- 在终端中优雅地编写Python
- Python机器学习实战:信用卡欺诈检测