Skip to content

Instantly share code, notes, and snippets.

@AiHiro
Created October 21, 2017 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AiHiro/c097174ca0bde807fff49b2ce1f00c1a to your computer and use it in GitHub Desktop.
Save AiHiro/c097174ca0bde807fff49b2ce1f00c1a to your computer and use it in GitHub Desktop.
import UIKit
class ViewController: UIViewController {
var countNum = 0
var timerRunning = false
var timer = Timer()
@objc func updateDisplay(){
countNum += 1
let ms = countNum % 100
let s = (countNum - ms) / 100 % 60
let m = (countNum - s - ms) / 6000 % 3600
timeDisplay.text = String(format: "%02d:%02d.%02d", m,s,ms)
}
@IBOutlet weak var timeDisplay: UILabel!
@IBAction func startButton(_ sender: UIButton) {
if timerRunning == false{
timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(ViewController.updateDisplay), userInfo: nil, repeats: true)
timerRunning = true
}
}
@IBAction func stopButton(_ sender: UIButton) {
if timerRunning == true{
timer.invalidate()
timerRunning = false
}
}
@IBAction func resetButton(_ sender: UIButton) {
countNum = 0
timeDisplay.text = "00:00.00"
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment