Created
August 6, 2016 11:09
-
-
Save samhann/e093430ab5531d46e65131fc9f6811df to your computer and use it in GitHub Desktop.
Stopwatch
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 | |
// Stopwatch new | |
// | |
// Created by Samhan on 06/08/16. | |
// Copyright © 2016 Samhan. All rights reserved. | |
// | |
import UIKit | |
protocol StopWatchDelegate : AnyObject | |
{ | |
func stopWatchTick(let string : String) | |
func stopWatchDidStart() | |
func stopWatchDidStop() | |
} | |
class Stopwatch | |
{ | |
var elapsedTimeSoFar : NSTimeInterval = 0 | |
var startTime : NSTimeInterval = 0 | |
weak var delegate : StopWatchDelegate? | |
func start(date : NSDate) | |
{ | |
self.startTime = NSDate.timeIntervalSinceReferenceDate() | |
self.updateDelegate(date) | |
self.delegate?.stopWatchDidStart() | |
} | |
func elapsed(date : NSDate) -> NSTimeInterval | |
{ | |
return date.timeIntervalSinceReferenceDate - self.startTime | |
} | |
func displayString( time : NSTimeInterval) -> String | |
{ | |
return "\(round(time * 10) / 10) sec" | |
} | |
func stop(date : NSDate) | |
{ | |
self.updateDelegate(date) | |
self.elapsedTimeSoFar = self.elapsedTimeSoFar + self.elapsed(date) | |
self.startTime = 0 | |
self.delegate?.stopWatchDidStop() | |
} | |
func updateDelegate(date : NSDate) | |
{ | |
self.delegate?.stopWatchTick(self.displayString(self.elapsed(date) + self.elapsedTimeSoFar)) | |
} | |
func tick(date : NSDate) | |
{ | |
self.updateDelegate(date) | |
} | |
} | |
class ViewController: UIViewController,StopWatchDelegate { | |
@IBOutlet weak var timeLabel: UILabel! | |
@IBOutlet weak var button: UIButton! | |
var timer : NSTimer? | |
var stopWatch : Stopwatch = Stopwatch() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
self.stopWatch.delegate = self | |
self.button.layer.cornerRadius = self.button.frame.width / 2 | |
} | |
@IBAction func buttonTapped(sender: AnyObject) { | |
if self.timer != nil { | |
self.timer?.invalidate() | |
self.stopWatch.stop(NSDate()) | |
self.timer = nil | |
} | |
else { | |
self.timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: #selector(tick), userInfo: nil, repeats: true) | |
self.stopWatch.start(NSDate()) | |
} | |
} | |
func stopWatchDidStart() | |
{ | |
self.button.backgroundColor = UIColor.greenColor() | |
self.button.setTitle("Stop", forState: .Normal) | |
} | |
func stopWatchDidStop() | |
{ | |
self.button.backgroundColor = UIColor.redColor() | |
self.button.setTitle("Start", forState: .Normal) | |
} | |
func tick() | |
{ | |
self.stopWatch.tick(NSDate()) | |
} | |
func stopWatchTick(let string : String) | |
{ | |
self.timeLabel.text = string | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment