Skip to content

Instantly share code, notes, and snippets.

Created August 26, 2014 02:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/0b63017efb3d3e0790c3 to your computer and use it in GitHub Desktop.
Save anonymous/0b63017efb3d3e0790c3 to your computer and use it in GitHub Desktop.
//
//
// ViewController.swift
// Stop Watch App
//
// Created by aGupieWare on 8/2/14.
// Copyright (c) 2014 aGupieWare. All rights reserved.
//
import UIKit
import QuartzCore
class ViewController: UIViewController {
@IBOutlet weak var numericDisplay: UILabel!
@IBOutlet weak var resetButton: UIButton!
@IBOutlet weak var startStopButton: UIButton!
var displayLink: CADisplayLink!
var lastDisplayLinkTimeStamp: CFTimeInterval!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Set default view element values //
self.numericDisplay.text = "0.00"
self.resetButton.setTitle("Reset", forState: UIControlState.Normal)
self.startStopButton.setTitle("Start", forState: UIControlState.Normal)
// Initializing the display link and directing it to call our displayLinkUpdate: method when an update is available //
self.displayLink = CADisplayLink(target: self, selector: "displayLinkUpdate:")
// Ensure that the display link is initially not updating //
self.displayLink.paused = true;
// Scheduling the Display Link to Send Notifications //
self.displayLink.addToRunLoop(NSRunLoop.mainRunLoop(), forMode: NSRunLoopCommonModes)
// Initial timestamp //
self.lastDisplayLinkTimeStamp = self.displayLink.timestamp
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func resetButtonPressed(sender: AnyObject) {
// Pause display link updates //
self.displayLink.paused = true;
// Set default numeric display value //
self.numericDisplay.text = "0.00"
// Set button to Start state //
self.startStopButton.setTitle("Start", forState: UIControlState.Normal)
// Reset our running tally //
self.lastDisplayLinkTimeStamp = 0.0
}
@IBAction func startStopButtonPressed(sender: AnyObject) {
// Toggle the display link's paused boolean value //
self.displayLink.paused = !(self.displayLink.paused)
// if the display link is not updating us... //
var buttonText:String = "Stop"
if self.displayLink.paused {
if self.lastDisplayLinkTimeStamp > 0 {
buttonText = "Resume"
}
else {
buttonText = "Start"
}
}
self.startStopButton.setTitle(buttonText, forState: UIControlState.Normal)
}
func displayLinkUpdate(sender: CADisplayLink) {
// Update running tally //
self.lastDisplayLinkTimeStamp = self.lastDisplayLinkTimeStamp + self.displayLink.duration
// Format the running tally to display on the last two significant digits //
let formattedString:String = String(format: "%0.2f", self.lastDisplayLinkTimeStamp)
// Display the formatted running tally //
self.numericDisplay.text = formattedString;
}
}
@chanqoDev
Copy link

For some reason when I click the "reset button" It goes back to "0.00"... but I pressed "Start" then it starts where the last timeStamp finished.... Any ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment