Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//
//
// 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

This comment has been minimized.

Copy link

chanqoDev commented Feb 10, 2015

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
You can’t perform that action at this time.