Countdown timer swift 3
I was looking for a countdown timer that made with swift 3 that display the countdown time in progress bar. Unfortunately I was not able to find a good library or a example project to do this. So I have to come up with my own solution. I have created a countdown timer using the help of CircleProgressView.
When you are creating a countdown timer there are two things to consider
- Calculate Remaining Time
- Display them in a nice way
Now we look into how i able to archive this. So as mention above first have to come up with calculating the remaining time. Things we have to worry about when we dealing with the date is time zone. But it this case you don’t have to worry about the time because we’re only consider about the remaining time (Date class working with UTC time format by default). Let’s see how to calculate the remaining time
initialized endTime as a global variable and assign the end time to the variabel
Now we have the end time so now lets see how to create the UI. To create the UI. first we need to add the circular progress bar from the pods.
pod 'CircleProgressView' add this line to the pod and install the pod after that create a UIView with XIB. Add 4 views to show Seconds, Minutes, Hours and Days, add CircleProgressView as the class of those view so it will look like this
Additionally use label to show remaining time as text. So finally your XIB should look like this
Now you have create the UI now we need to update the ui and it should continuously update. for this we can you a Timer
You can call setTimeLeft Method inside the timer so it will update remaining time. And you can also update your UI inside form the setTimeLeft Method. So your final code look like this
So final result looks like below
I have host the sample project in github download or clone the project and give a pod install.