Stop Watch App with play & pause buttons using Xcode and Swift 2.0

Stop Watch App with play & pause buttons using Xcode and Swift 2.0
10th October 2015 Shane

Create and swap out play and pause buttons programmatically using Xcode and Swift while developing a simple Stop Watch App.

Create play and pause buttons programmatically using swift while making a stop watch app as an example. This video shows you how to swap out and replace a play button with a pause button on press. Using UIBarButtonItem buttons on a toolbar or navigation bar using Xcode and Apple’s programming language Swift 2.0. This video Also shows how to use Xcode’s auto layout to keep your interface elements in place using portrait or landscape orientations.

You will also see how how to create, start, stop, pause & reset a timer and create IBActions and buttons using code.

All while I try out my new Blue Snowball Mic.

As always I have included a download link to the full Xcode 7 project with comments, which you can find under the video below and have also posted the ViewController Code.

Note: This video was an inspired by a challenge to further enhance an app from a Udemy Course by Rob Percival, there are a few hints and tips as to how to solve the problem of programmatically creating play & pause buttons in the discussions. Although there is no complete solution. So, I thought I would create one.

Rob’s course mentioned above is designed for iOS8, however, Rob has now created a course specifically for iOS 9, and all the code I have written works in Xcode 7, Swift 2.0 and iOS 9.0.2.

You can find Rob’s new iOS9 course here: https://www.udemy.com/the-complete-ios-9-developer-course/

Which I highly recommended.

OK on with the video…

Creating a StopWatch App using Play and Pause buttons created programmatically.

Here’s the ViewController code

//
// ViewController.swift
// StopWatch
//
// Created by Shane Hogan on 07/09/2015.
// Copyright (c) 2015 Shane Hogan. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var time: UILabel!

@IBOutlet weak var toolbar: UIToolbar!

var stopButton: UIBarButtonItem {

return UIBarButtonItem (barButtonSystemItem: .Stop, target: self, action: “stop:”)

}

var playButton: UIBarButtonItem {

return UIBarButtonItem (barButtonSystemItem: .Play, target: self, action: “play:”)

}

var pauseButton: UIBarButtonItem {

return UIBarButtonItem (barButtonSystemItem: .Pause, target: self, action: “pause:”)

}

var flexibleSpaceBar: UIBarButtonItem {

return UIBarButtonItem (barButtonSystemItem: .FlexibleSpace, target: self, action: nil)

}

var timer = NSTimer()

var secondsCounter:Int = 0

var isTimerStarted:Bool = false

@IBAction func play(sender: AnyObject) {

isTimerStarted = true
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: (“updateTime”), userInfo: nil, repeats: true)
showPlayOrPause()
}

@IBAction func pause(sender: AnyObject) {

isTimerStarted = false
timer.invalidate()
showPlayOrPause()
}

@IBAction func stop(sender: AnyObject) {

isTimerStarted = false
timer.invalidate()
secondsCounter = 0
time.text = “0”
showPlayOrPause()
}

func updateTime() {

time.text = String(secondsCounter)
secondsCounter++
}

func showPlayOrPause() {

var showPlayOrPauseButton: UIBarButtonItem

if isTimerStarted {

showPlayOrPauseButton = pauseButton

} else {

showPlayOrPauseButton = playButton

}

let placeToolbarButtonItems = [stopButton, flexibleSpaceBar, showPlayOrPauseButton]
toolbar.setItems(placeToolbarButtonItems, animated: true)

}

override func viewDidLoad() {
super.viewDidLoad()

time.text = String(“0”)
showPlayOrPause()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}

Download the Xcode 7 Swift 2.0 project here: StopWatch

Buy ScaleViz To help you Master the Modes, Nail the CAGED system, learn all Major, Minor and Dominant, Chords, Arpeggios, Pentatonics and Modal Shapes in all positions, go from From Cowboy Chords to Soloing and become a Fretboard Master with ScaleViz today!