Created
August 26, 2014 02:25
-
-
Save anonymous/0b63017efb3d3e0790c3 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
// | |
// | |
// 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?