Last active
September 4, 2016 18:59
-
-
Save Calvin-Huang/c5a3d53fb88f74fa46f7305cb1eedb08 to your computer and use it in GitHub Desktop.
The basic counter. https://github.com/Calvin-Huang/CHCountingLabel/blob/d65639635fef7f3c6e07ce8eb3fbaa4804ca8f33/Animation-Issue/Base.lproj/Main.storyboard
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
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