Create A Stopwatch Demo In Swift 2.0 In A Simple Way

Inwizards-blog-img

Create a new Project:

Create new single view application Xcode project by naming as ”StopWatchDemoSwift”. Set the values as follows and save it as a folder on your given destination.(Optional on Desktop)

Begin the Project:

Go to the mainStoryboard and Add two buttons: First button for Start and Stop functionality and the Second button for lap showing and resetting the Stopwatch. and then add Tableview for displaying your laps data to the main storyboard from the Object library.

How it works:

First on clicking “Start” button your stopwatch start.Then on clicking “Lap” button your laps displaying into the tableview.Then on clicking “Stop” button your running label stops. And after if you click the “Reset” button the label show its default value.

Here is the complete code in ViewController.swift file:

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {



// outlet made for tableview to display laps.

@IBOutlet weak var lapsTableView: UITableView!

// outlet made for tableview to display stopwatch label

@IBOutlet weak var stopwatchLabel: UILabel!

//outlet made for start and stop the lap time

@IBOutlet weak var startStopButton: UIButton!

// outlet made for resetting and to display lap on tapping while the given lap time running

@IBOutlet weak var resetLapButton: UIButton!

var laps : [String] = []

var stopwatchString: String = “”

var timer = NSTimer()

var minutes:Int = 0

var seconds: Int = 0



var fractions:Int = 0

var stopResetWatch:Bool = true

var addLap:Bool = false

override func viewDidLoad() {

super.viewDidLoad()

stopwatchLabel.text = “00:00.00″

// Do any additional setup after loading the view, typically from a nib.

}

// function made for stopwatch for Starting and Stopping it.

@IBAction func startStop(sender: AnyObject) {

if stopResetWatch == true{

timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: Selector(“updateStopwatch”), userInfo: nil, repeats: true)

stopResetWatch = false

addLap = true

}else{

timer.invalidate()

stopResetWatch = true

addLap = false

}

}

// function made for resetting the Stopwatch

@IBAction func resetLap(sender: AnyObject) {

if addLap == true{

laps.insert(stopwatchString, atIndex: 0)

lapsTableView.reloadData()

}else{

addLap = false

laps.removeAll(keepCapacity: false)

lapsTableView.reloadData()

fractions = 0

seconds = 0

minutes = 0

stopwatchString = “00:00.00″



stopwatchLabel.text = stopwatchString

}

}

// function made for updation in stopwatch

func updateStopwatch(){

fractions += 1

if fractions == 100{

seconds += 1

fractions = 0

}

if seconds == 60{

minutes += 1

seconds = 0

}

let fractionsString = fractions > 9 ? \(fractions)”:“0\(fractions)”

let secondsString = seconds > 9 ? \(seconds)”:“0\(seconds)”

let minutesString = minutes > 9 ? \(minutes)”:“0\(minutes)”

stopwatchString = \(minutesString):\(secondsString).\(fractionsString)”

stopwatchLabel.text = stopwatchString

}



// methods made for tableview

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return laps.count

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = UITableViewCell(style:UITableViewCellStyle.Value1,reuseIdentifier: “Cell”)

cell.backgroundColor = self.view.backgroundColor

cell.textLabel?.text = “Lap \(laps.count-indexPath.row)”

cell.detailTextLabel?.text = laps[indexPath.row]

return cell

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}

you can download Demo Project Here :

https://goo.gl/cM55AH

Enjoy… 🙂



Create A Stopwatch Demo In Swift 2.0 In A Simple Way

Post navigation


0 0 vote
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
minecraf
minecraf
7 years ago

nice posts, the good article haha!download free now

1
0
Would love your thoughts, please comment.x
()
x