1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Advanced iOS Features: Permanent Features, Segue, Alert Message, Timers, and Gestures

Timers

Contents

keyboard_tab
Advanced iOS Features
1
Introduction
PREVIEW31s
2
Storing Data
PREVIEW11m 37s
5
Second Screen
PREVIEW8m 59s
6
Segue
PREVIEW8m 6s
12
Timers
14m 39s

The course is part of this learning path

Start course
Overview
Difficulty
Beginner
Duration
1h 57m
Students
8
Description

This course explores some advanced iOS features. We'll look at how to store data permanently. You'll learn how to work with more than just one screen, how to use Gesture Recognizers, Timers, Alert Messages and so much more. You're going to have essential skills for iOS development after you complete this course.

Intended Audience

This course is intended for beginners who want to learn how to build apps using Swift.

Prerequisites

To the most out of this course, you should have some basic understanding of programming and computer science in general.

 

Transcript

Hi, within this lecture, we're going to learn about timers. So, timers are some structures that allow us to do some operation repetitively within a given period of time and it runs this operation in the background. So, you will see what I mean by telling background and you will see what I mean by telling within a given period of time. So, in order to do that, I'm going to create a single-view app. I'm going to call this TimerProject simply and I'm going to hit 'Next' to create this project and save it on my iOS folder. After that, let me make this project into a big full screen and then go to ViewController.swift. So here, we're going to define our timer object. And suppose that I want to have a countdown timer, I want to count from 10 to 0. 

So, let's suppose that I'm making a game in which we are competing against time. So, I will need a time label in which I will count from back to 10, 9, 8, 7 and it goes all the way to the 0. In order to do that, we're going to create a timer and you do it like this. So, with big Timer, with capitalized t, and says that this is a timer that fires after a certain time interval has elapsed. So, if I say one second, for example, it will send a specified message to a target object, for example, to ViewController. If you hit 'comment' and click over here, you can jump to definition or you can go to quick help to see the documentation file. And if you click over here, to 'Open in Developer Documentation, it displays a more detailed message to you. So, if you read over here, you will see that it runs something called run loops. And this maintains strong references to your timers. So, what does it mean?

And in the below, you will see something called Threading Programming Guide. So, it uses some threads. So, what is a thread? So, this kind of a collection that you run your operation on. So, if we are running everything on a single thread, if we are doing something that can block the other operations, then the current thread will only work on one operation and we cannot do something else during that period. For example, if I try to use some conventional methods other than timers, in order to run a timer that counts back from 10 to 0, for that period of time, I cannot do anything else. I cannot interact with the user interface, I cannot click on anything, I cannot see what's happening on my user interface. In the background maybe it's running from 10 to 0, but I cannot see it. 

So, let me go and create this design first. So, let me have a label in which I will have a time variable which counts back from 10. So, let me call this Time. And this will be counting down from 10, but let me just for now, make it like this and create a button and then make it a little bit bigger, and this will start our timer. I don't need anything else to complete this app, so I'm going to leave it as it is. And before we actually define these views into our review controller, let me 'Reset to Suggested Constraints' and then open an assistant editor in which I will call ViewController.swift. And here, I will take this time label and define as a variable over here, I will call this timeLabel. And also, I can just drag and drop this button and for action, I will say buttonClicked. So, that's it. It's going to be very simple. 

So, let me say print("button clicked"), under this buttonClicked function. So, that's it. Let me close this down and we're done designing this app. Let me go to ViewController.swift. I'm going to first count down in a way that you will think that will work, if you don't know about timers. So, first of all, let me run the simulator. And you will see if I click on this button, I see button clicked. Now, if you didn't know about timers, maybe you would think that, "Yeah, I can do this in a for loop. I can create a for Loop that will run 10 times." So, in this for loop, if I can make a way to wait for one second, then it means that I get to run this loop for 10 times and it will wait one second between each loop. So, I will count back from 10 to 1 or 10 to 0, and that's a good idea, but it's not going to work. 

So, let me do it for you. So, I'm going to say for, and I'm going to come up with a variable name in here, like time, and I will say, in 1...10. So, we have seen this before. So, it's going to run this loop 10 times. And we're not using time variable in this case, but it doesn't matter. I'm just going to say timeLabel is something like, 10, 9. And in order to do that, of course we're going to have to come up with a variable because in this variable, I'm going to deduct one. So, if I create a counter variable here, I can set it to be zero, initially 10, I can deduct  one each time this loop runs. So, in the viewDidLload, I'm going to say, counter is 10. And in the loop, in the for loop, I'm going to say, counter is counter - 1. So, this will deduct one from counter every time this for loop runs and it will display this counter in my timeLabel.

So, this may be your initial thought if you don't know about timers. And then later on, we're going to have to come up with a way to wait for one second. Because if I run this right now, I will just see zero because it will count back from 10 to 1 even before we see the view. It will just happen instantaneously, we won't see it. So, in order to make this work, maybe we can come up with sleeping for one second and actually there is a way to do that. If I can come over here in my for loop, I can say Thread.sleep. And again, thread is where we run our operations. So, if I sleep for given TimeInterval, for example, one second, it will just block everything for one second and then continue running this loop. But this won't work. So, we're going to wait here for 10 seconds before we see the view and then we're going to see the zero, as you can see. Because maybe you can think that, because you run that in the viewDidLoad. So, viewDidLoad actually gets called before we see the view. Maybe you have to run this and view will appear, or viewDid appear. 

So, let's try to do that for 100 seconds, for example. And this will make things even worse because we will not be seeing this timeLabel for like 600, no 1000 seconds. So, we're going to have to wait for 1000 seconds in order to see the view right now. So, this doesn't work. So, the next thing is to try the view will appear or viewDidAppear think, so that we can be sure that view actually did load and we see the view. Now, it's time to do this operation. And in order to do that, I'm going to call the function first in here. If you say view, you will see the viewDid appear. So, I'm going to take everything in here and cut with command x and then paste under my viewDidAppear. So, I'm certain that this will get run after I see the view itself. So, let's do it this way. So, let's start. And as you can see, we see the time label right now but we cannot see the time and I cannot click on the button. So, as I said before, it blocks the entire operation. So, user cannot interact with the user interface within that given period of time. So, for 10 seconds I couldn't do anything. 

So, we're going to go ahead and do this operation in another thread. And in order to do that, we're going to use timers. So timers, dramatically manage that for us. We can do this operation in the background, so user can interact with the user interface. The other operations will continue as it is, but also, we will do counter -1 in a given period of time repetitively and it will be perfect. It will be in a way that we want. So, let me see how timers are created. So, we have defined our timer but we have to make sure that it's fired as well. So, for right now, I'm going to say timeLabel.text is actually time counter. And then later on, I'm going to change my timer value in a way that I want. What do I mean? If you come over here with small timer object that you have created, you can say Timer.scheduledTimer. So, this will ask you for a couple of parameters and we want this one. So, we want but not this one, but this one. TimeInterval and target and selector, userInfo and repeat. So, we have a lot of parameters here. Let me explain one by one. So, timeInterval is the real interval that we want this operation to be carried on. 

So, I want every second. And the target will be self one more time because that's where we are going to write this selector function. So, selector function will be called each second, every second. So, let me write it down. Objc func timerFunction, and whatever I write here will be executed for every second. So, this will be timer function or you can just write Viewcontroller timerFunction. User info, we don't want to send any user info. So, I'm going to say nil. And does this repeat? Yes, this repeats. And we can call it whenever we want, we can stop it, we can restart it, and this repeats. So, that's it. We actually finished our timer initialization. So, under timer function, what I want to happen is to just go over here and say timeLabel.text is again, Time and \counter. And then, I want to say counter is counter -1. So, you can say this way, counter -=1. And then later on, do I want to do anything?

Yes, I want to check to see if counter is and if it's 0, I want to stop this timer from going on. So, I can say timeLabel.text is now time 0 or Time's Over. I can come over here, I can say timer.invalidate, in order to stop this timer from going on. So, that's good. We're checking to see if timer has reached and we are invalidating our timer. So, let's start this and let's see if this works. As you can see, it counts down and we're seeing this and we can actually click on that button as well. So, it's not blocking the user interface, it's doing its job in the background just like we want. So, that's how you work with timers. And you can do any operation with timers. Not only create countdown timers, you can do whatever you want inside of this function, if you want it repetitively for a given period of time. So, that's it. We're going to stop here and within the next section, we're going to write our first game.

 

About the Author
Students
437
Courses
55
Learning Paths
3

Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.

Covered Topics