Created
October 21, 2017 09:27
-
-
Save AiHiro/c097174ca0bde807fff49b2ce1f00c1a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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