import UIKit
class ViewController: UIViewController {
var times = 5
var startBtn = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
self.createButton()
// 三秒后执行
self.perform(#selector(sanmiaohou), with: nil, afterDelay: 3)
}
func sanmiaohou(){
print("三秒后执行")
}
var totalTimer:Timer?
func createButton() {
let button = UIButton(frame: CGRect(x: 20, y: 100, width: 100, height: 40))
self.view.addSubview(button)
button.backgroundColor = UIColor.red
button.addTarget(self, action: #selector(createTimer), for: .touchUpInside)
button.setTitle("开始", for: .normal)
self.startBtn = button
let button2 = UIButton(frame: CGRect(x: 140, y: 100, width: 100, height: 40))
self.view.addSubview(button2)
button2.backgroundColor = UIColor.red
button2.addTarget(self, action: #selector(endTimerAction), for: .touchUpInside)
button2.setTitle("完全结束", for: .normal)
let button3 = UIButton(frame: CGRect(x: 20, y: 250, width: 100, height: 40))
self.view.addSubview(button3)
button3.backgroundColor = UIColor.red
button3.addTarget(self, action: #selector(createRoopTimer), for: .touchUpInside)
button3.setTitle("子线程开启", for: .normal)
let button21 = UIButton(frame: CGRect(x: 140, y: 200, width: 100, height: 40))
self.view.addSubview(button21)
button21.backgroundColor = UIColor.red
button21.addTarget(self, action: #selector(continueTimerAction2), for: .touchUpInside)
button21.setTitle("接着开始", for: .normal)
let button213 = UIButton(frame: CGRect(x: 140, y: 320, width: 100, height: 40))
self.view.addSubview(button213)
button213.backgroundColor = UIColor.red
button213.addTarget(self, action: #selector(stopTimerAction2), for: .touchUpInside)
button213.setTitle("暂停", for: .normal)
}
// 方法名以scheduled开头的均不需要手动加入NSRunLoop
func createTimer() {
/*参数:TimeInterval:触发时间,单位秒
target:定时起触发对象
selector:定时器响应方法
userInfo:用户信息
repeats:是否重复执行,YES 每个指定的时间重复执行,NO 只执行一次*/
let timer:Timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(startTimerAction), userInfo: nil, repeats: true)
self.totalTimer = timer
self.totalTimer?.fire()
}
func startTimerAction() {
print("timer start开始%ld",times)
times = times - 1
self.startBtn.setTitle("\(times)", for: .normal)
if times == 0 {
self.startBtn.setTitle("结束", for: .normal)
print("定时器结束")
self.totalTimer?.invalidate()
times = 5
}
}
func endTimerAction() {
self.totalTimer?.invalidate()
print("timer end完全结束")
}
//自动子线程定时器的创建
func createRoopTimer() {
// 在子线程创建定时器
/*scheduled 或 timer 方式创建*/
let timer:Timer = Timer(timeInterval: 1.0,
target: self,
selector: #selector(self.updateTimer(timer:)),
userInfo: nil,
repeats: true)
//这句话实现多线程,如果你的ScrollView是作为TableView的headerView的话,在拖动tableView的时候让轮播图仍然能轮播就需要用到这句话
RunLoop.current.add(timer, forMode: RunLoopMode.commonModes)
self.totalTimer = timer
}
var num:Int = 10
func updateTimer(timer:Timer) {
num = num + 1
print("\(num)")
// 满足条件后,停止当前的运行循环
if (num == 15) {
// 停止当前的运行循环
/*一旦停止了运行循环,后续代码能够执行,执行完毕后,线程被自动销毁*/
print("自线程停止", num)
num = 10
}
}
func stopTimerAction2(timer:Timer) {
self.totalTimer?.fireDate = NSDate.distantFuture
print("暂停timer end")
}
func continueTimerAction2() {
self.totalTimer?.fireDate = NSDate.distantPast
print("接着开始timer start")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}