iOS Tutorials: Making your own timer app

If you ever wanted to make an app that could keep track of the time, this is one of the many best way you could implement it. The timer function has the capabilities of starting the timer, pausing the timer or reset the timer.

On your Xcode, go to File > New > Project to create a new blank project.

Go ahead and give your project any name. For mine, I put Timer. You could go ahead and put in the required information as your own information.

By now you should know how to grab object from the object library. If not, it is located on the bottom right. For this, we are going to need 3 buttons and 1 label. Go ahead and place them accordingly and name them.

On the top right, click Assistant Editor to get split screen. Basically what you are seeing right now is that the class on the right holds the code that run the layout on the left.

Now click Control on each of the button and drag it to the code side

It should then pop up where you are able to pick your selection, replace Outlet with Action. Make sure that Start, Pause and Reset has @IBAction

You should have:

@IBAction for StartButton, I named it startPressed

@IBAction for PauseButton, I named it pausePressed

@IBAction for ResetButton, I named it resetPressed

@IBOutlet for StartButton, I named it startButton

@IBOutlet for PauseButton, I named it pauseButton

@IBOutlet for the label, I named it timeLabel

If you are having trouble, your code should look like this:

import UIKit
 
class ViewController: UIViewController {
    @IBOutlet weak var startButton: UIButton!
    @IBOutlet weak var pauseButton: UIButton!
    
    
    @IBOutlet weak var timeLabel: UILabel!
    
    var counter = 0.0
    var time = Timer()
    var isCounting = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    @IBAction func startPressed(_ sender: Any) {
        if (isCounting){
            return
        }
        startButton.isEnabled = false
        pauseButton.isEnabled = true
        time = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(ViewController.startCounting), userInfo: nil, repeats: true)
        isCounting = true
    }
 
    @IBAction func pausePressed(_ sender: Any) {
        pauseButton.isEnabled = false
        startButton.isEnabled = true
        time.invalidate()
        isCounting = false
    }
    
 
    
    @IBAction func resetPressed(_ sender: Any) {
        isCounting = false
        time.invalidate()
        counter = 0
        timeLabel.text = String(counter)
        startButton.isEnabled = true
        pauseButton.isEnabled = true
    }
    
    func startCounting() {
        counter = counter + 0.1
        timeLabel.text = String(format: "%.1f", counter)
    }
    
}

Your end result should be like this. If you are having problem with it, download my code from GitHub

  • Article By :
    Founder of DaddyCoding. Studied Computer Science, Information System and Information Technology at BYU-Hawaii. Currently spending most of my time researching and learning on helping to expose making iOS apps.

Random Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*