Skip to content

Instantly share code, notes, and snippets.

@Calvin-Huang
Last active September 4, 2016 18:59
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 Calvin-Huang/c5a3d53fb88f74fa46f7305cb1eedb08 to your computer and use it in GitHub Desktop.
Save Calvin-Huang/c5a3d53fb88f74fa46f7305cb1eedb08 to your computer and use it in GitHub Desktop.
class ViewController: UIViewController {
@IBOutlet weak var countingLabel: UILabel!
private var timer: NSTimer?
private var startTimeStamp: Int = 0
private var startCountingValue: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func startCountingButtonClicked(_: AnyObject) {
if timer == nil {
guard let labelText = self.countingLabel.text, let currentValue = Int(labelText) else {
return
}
startTimeStamp = Int(NSDate().timeIntervalSince1970)
startCountingValue = currentValue
timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: #selector(updateCountingLabel(_:)), userInfo: nil, repeats: true)
}
}
// MARK: - Selectors
func updateCountingLabel(_: NSTimer) {
let currentTimeStamp = Int(NSDate().timeIntervalSince1970)
self.countingLabel.text = String(startCountingValue - (currentTimeStamp - startTimeStamp))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment