How to make a Countdown Timer in iOS using Swift

How to make a Countdown Timer in iOS using Swift

Today, I will show how to build a timer that countdowns until an event (e.g. new year’s day or release date of a video game) using the scheduledTimer of the Timer class and refresh it every second.

First, we set the current date using the calendar that user use on their device.

We set the event time we want:

If an event is happening at a particular time (e.g. a tv show starts at 8:00PM GMT) we can add the time zone alongside the date of this event.

We convert the event date to the user’s calendar:

We change the seconds to days, hours, minutes and seconds:

…and we display the results into our label:

Last, we add the Timer in our viewDidLoad(), and set it to repeat the method every second:

If you want to change the label after the event has passed, you can do this by checking if the current date is after the event. If yes, then will stop the timer and show the text.

So at the end, our ViewController looks like this:

You can find the final project here

If you have any questions feel free to DM me on Twitter @johncodeos or leave a comment below!

Leave a Reply

avatar
  Subscribe  
Notify of

Follow me