1 minute read

Build a simple stopwatch app with Timer in Swift

A stopwatch is small enough to build in an afternoon, but it still exercises several core iOS skills: state management, accurate elapsed-time calculation, UIKit layout, and lifecycle handling. The finished app needs only a time display, a start button, a reset button, and a reliable update mechanism.

The central decision is where the truth lives. A timer callback can tell the app when to refresh the screen, but it should not be the only record of elapsed time. By separating the time model from the interface, the stopwatch remains accurate when callbacks are delayed or the app returns from the background.

Model elapsed time before drawing the interface

Start by defining the states a stopwatch can occupy: stopped, running, and paused. Keep an accumulated value for time already recorded, plus an optional startedAt date that marks the beginning of the current running segment. The displayed elapsed time is the accumulated value plus the time since startedAt.

This approach avoids a fragile counter that increments every time a callback fires. Callbacks may arrive late while the device is busy, so adding 0.1 each time can gradually produce the wrong result. Calculating the difference between two dates preserves accuracy even when an update is skipped.

When the user starts the stopwatch, save the current date. Pausing should copy the calculated elapsed time into accumulated, clear startedAt, and stop the timer. Resetting clears both values. This model remains simple while providing a clean foundation for later features such as laps or split times.

Create a focused UIKit interface

UIKit works well here because the interface has only a few controls. A vertical stack view can contain a large UILabel, a start or pause button, and a reset button. Constraints can centre this stack on the screen and keep enough spacing between the time and the controls.

Use a monospaced-digit system font for the label so the numbers do not shift as their values change. A format such as 00:00.0 provides useful precision without displaying distracting hundredths. Also enable automatic font scaling and test that the longest expected value still fits when the user has enlarged text in iOS Settings.

A simple button-state update keeps the interface understandable. Change the first button between “Start” and “Pause”, and disable it while the stopwatch is stopped after a reset. Clear labels, visible focus, and touch targets of at least 44 points make the app comfortable for commuters checking a phone between stations in Sydney or Melbourne.

Drive the display with Foundation Timer

Foundation.Timer can schedule a repeating callback that asks the interface to refresh. A tenth-of-a-second interval is suitable for a basic stopwatch because the visual display does not need to update hundreds of times per second. The callback should calculate elapsed time again and update the label rather than changing the stored duration itself.

Create the timer with Timer.scheduledTimer(withTimeInterval:repeats:), capture the view controller weakly in its closure, and invalidate the timer when it is no longer needed. Weak capture prevents the timer from extending the lifetime of the controller accidentally. Invalidating it also stops unnecessary work while the stopwatch is paused.

The same separation appears in embedded NetBeans setup, where device configuration is handled separately from application logic. Swift follows a similar principle: the timer schedules UI work, while the date-based model owns the meaning of elapsed time.

Keep timing correct through app lifecycle

iOS suspends timers when an app enters the background, so a timer cannot be treated as a continuous clock. Fortunately, the date-based model already accounts for this. When the app becomes active again, calculate the elapsed time from startedAt and update the label immediately, regardless of how many timer callbacks were missed.

This behaviour also makes the app useful during a busy morning in Brisbane, when the phone may be handling navigation, calls, and notifications while the stopwatch continues. The user should still see the correct duration after unlocking the device because the app is deriving the result from absolute dates.

Pause the stopwatch when the app moves to the background if the intended meaning is that time only counts while the app is visible. Otherwise, leave startedAt unchanged and refresh on return. Whichever policy is chosen should be documented, because it determines whether the stopwatch measures wall-clock time or foreground-only time.

Test for Australian users and real devices

Test on several screen sizes and with Dynamic Type enabled. The time label should remain readable on a small iPhone, rotate on a larger device, and adapt to light and dark appearance. VoiceOver should announce the button names and changes to the displayed time without relying on colour alone.

Australian locations also expose useful time-zone edge cases. Sydney, Melbourne, Adelaide, Canberra, Hobart, and Brisbane observe daylight-saving transitions at different times or follow different rules. An elapsed-time calculation using Date handles these transitions automatically because it measures an interval rather than adding local clock minutes.

If the app becomes commercial, remember that the Australian market appears in App Store Connect with prices in Australian dollars, and consumer purchases are covered by the Australian Consumer Law. Privacy requirements also matter: the Privacy Act 1988 becomes relevant if analytics identify users, while a local stopwatch that records nothing can remain deliberately simple and permission-free.

Polish the project for a portfolio release

A small codebase can still demonstrate sound engineering. Organise the elapsed-time model, view controller, and formatting logic clearly, then add a README that explains the design choices. The README can state why the app calculates from dates, how pausing works, and which devices were tested.

For a portfolio, record a short demonstration showing a timed interval, background recovery, and an accessibility pass. Publishing the source to GitHub gives reviewers a way to inspect the implementation rather than judging only a promotional video. Clear commit messages and a focused README make the project easier for another junior developer to understand.

The next concrete step is to create a fresh UIKit project named Stopwatch and run it with a ten-second stopwatch.

Updated: