Since the countdown function was implemented when creating a personal application, We will share the command after the memorandum.
Please take a look at the code on GitHub. -> https://github.com/onishi-app/CountDown
environment ・ Apple Swift version 5.3 ・ XCode version 12.3
The completed form this time is as follows. ** After tapping the button, it counts down by 1 second and the screen changes when it reaches 0 seconds. ** **
This time, with ViewController.swift, which counts down, I have defined NextViewController.swift as the screen transition destination. Since there is no particular processing in NextViewController.swift, it will be omitted.
I would like to use the Timer class to implement the countdown.
ViewController.swift
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var countLabel: UILabel!
@IBOutlet weak var startButton: UIButton!
var time = 5
var timer = Timer()
override func viewDidLoad() {
super.viewDidLoad()
countLabel.text = String(time)
startButton.layer.cornerRadius = 10
}
//Processing when the button is pressed
@IBAction func buttonAction(_ sender: Any) {
timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { (timer) in
self.time -= 1
self.countLabel.text = String(self.time)
if self.time == 0 {
self.performSegue(withIdentifier: "next", sender: nil)
}
})
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
timer.invalidate()
}
}
Nothing special is done in viewDidLoad ()
.
It is the value of the text variable timer of Label, and the corners of the button are rounded.
ViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
countLabel.text = String(time) //text change
startButton.layer.cornerRadius = 10 //Rounded corner
}
I want to start the countdown when the start button is pressed, so
Defined in @IBAction func buttonAction (_ sender: Any) {}
.
Use the scheduledTimer ()
method of the Timer class.
Set withTimeInterval to 1 second and repeats to true.
Although it is a process in the method, the time value is decremented by 1 and reassigned to the Label value. By this process, the Label value will also decrease by 1 second 1 second after the button is pressed.
Since the screen transition will be performed when it reaches 0 seconds,
The screen transition is performed at the location of if self.time == 0 {・ ・ ・}
.
ViewController.swift
//Processing when the button is pressed
@IBAction func buttonAction(_ sender: Any) {
timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { (timer) in
self.time -= 1
self.countLabel.text = String(self.time)
if self.time == 0 {
self.performSegue(withIdentifier: "next", sender: nil)
}
})
}
Since I want to stop the timer when performing a screen transition, the following command is described.
prepare (for segue: UIStoryboardSegue, sender: Any?) {}
Is
This method is called when the screen is changed by performSegue ()
.
In the method, timer.invalidate ()
is executed to stop the timer.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
timer.invalidate()
}
The countdown function is fairly easy to implement, so please use it.
You can also use the Timer class to implement the functionality of an iPhone stopwatch. (I don't think it can be implemented only with the Timer class ...)
Thank you for watching until the end.
Recommended Posts