Created
March 2, 2017 05:57
-
-
Save paulw11/3ce44b6f7e2a29db3ba28d1ce3314e7e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// | |
// ViewController.swift | |
// timertestswift | |
// | |
// Created by Paul Wilkinson on 2/3/17. | |
// Copyright © 2017 Paul Wilkinson. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet weak var label: UILabel! | |
@IBOutlet weak var countDownLabel: UILabel! | |
let dateFormatter = DateFormatter() | |
var targetTime: Date! | |
var timer: Timer? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
let targetTime = Date().addingTimeInterval(120) | |
let calendar = Calendar.current | |
var components = calendar.dateComponents([.day, .month, .year, .hour, .minute, .second, .nanosecond], from: targetTime) | |
components.setValue(0, for: .second) | |
self.targetTime = calendar.date(from: components) | |
self.timer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true, block: { (timer) in | |
self.updateLabel() | |
}) | |
self.dateFormatter.timeStyle = .long | |
self.dateFormatter.dateStyle = .short | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
func updateLabel() { | |
let now = Date() | |
self.label.text = self.dateFormatter.string(from: now) | |
let timeRemaining = round(self.targetTime.timeIntervalSince(now)) | |
if timeRemaining > 0 { | |
self.countDownLabel.text = "\(timeRemaining)" | |
} else { | |
self.countDownLabel.text = "0" | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment