Radio Inspire

How To Learn Sign Language

Swift 1.5 Tutorial – Add Tap Gestures to Animate Refactored Code from a Method

– [Paul] Hi, this is Paul
Solt from iPhoneDev.TV. We’re going to go ahead and
make it so we can resuse this animation multiple times
so it can actually repeat. So right now with the application, if I were to tap anywhere
in here nothing happens, so it only happens when we hit the play button up in the top corner. We’ll see the animation repeat itself, and we can keep repeating that, but this isn’t very fun
if you make this app and then you go to show someone because it won’t do the animation again. So we’re going to go ahead
and get that to refactor it, so we can take that animation code, put it in a chunk of code called a method that we can reuse in different
parts of our application. So we can reuse it when our
application first starts, and we can reuse it when the
user will tap on the screen. So the first thing that we’re going to do here is add a tap gesture, which is a really cool class that allows you to interact with the user and get single taps or multiple taps. There’s other things like
a pan gesture for dragging an object around the
screen and stuff like that. So let’s go ahead and get started. We don’t need that popup, and the first thing that
I’m going to do down here is we’re going to find
the view to load method, and you’ll go all the
way to the bottom of it, which should be around line 50, and insert some new code. So let’s create a new variable. This is going to be our tap
gesture right down here, and that’s going to be a
UI tap gesture recognizer. Now, what you’re going to want to do here is do an open parentheses, and you’ll get some code assistance. And the thing we want is
that target action selector, so if we go ahead and just press enter it will autocomplete for us. It highlights the first thing. Now, the target is going to to be who’s going to respond to this message? And this is part of the
target action design pattern that’s part of the Cocoa Touch APIs, and so what we have
here is we have someone who’s going to be
responsible for a message, and that message is
going to be our selector. So the first thing we need
to do is just pass self. Self means this current object, which is the view controller, which you can see right here. So if I press enter
we’re all good on that. I’ll hit tab to go to the selector. Now the selector is going
to be the name of the method or the name of the function
that’s going to be called, and that needs to be
within this code file. So right now we don’t have one to find, but we’re going to make one. Now, there are a couple of gotchas here, and in another video after this one I’m going to go over the different issues that you can run into and
how to recover from them. For now just try to follow along, and if you do get stuck,
skip to the next video where I’ll show you how to recover. So the first thing we do
here is type selector, and then we do an open parentheses, and then we give a name
here in a string format. So it has to have the
double quotation marks, and this is going to be
called Handle Tap Gesture, and I’m going to put a
colon on the end of it. I’ll move the cursor over one two times, and now you’ll see that
we have an error here. We need to close the opening parentheses, and you can see that that matches, so we’re good to go here. Next, we need to add this to the view. So the view can hold a tap
gesture and respond to it, so the view is our screen right now. So that’s what we see. That’s that white screen that we see. And if we say view dot
add gesture recognizer, we can go ahead and add the tap gesture. So go ahead and hit tab, and
then fill in tap gesture, and close the parentheses. So now we’ve added a method. If I were to run this right
now it would not work, so let’s go ahead and
make a method definition that’s going to match the signature that we have for this selector. So what that means is,
we need to copy and paste this text and make sure we
create a function with that. So we’re going to use
the function keyword. I’m going to paste what I just copied, which is the name of the method, but in Swift we actually have
an open parentheses first, and then in here we are going
to have our tap gesture, which is a perimeter that
is going to be passed to us, and this is going to be a
UI tap gesture recognizer, so we’re creating this out of thin air. This is our method to define
what’s going to happen, and here we can just print out a message using the print line command and say tap. O.K., so when you see handle tap gesture that needs to match here. When you see a colon that means we have another perimeter and that
needs to match right here, so we’re going to go ahead and hit the run option right up
top and see what happens. Alright, so everything worked well. If you typed exactly what I typed, it should work for you. Otherwise, you might want
to check the next video for how to solve some
of the common problems. So now that we have a way to
tap anywhere on the screen to invoke an action we can use that to cause the animation to repeat, but there’s a catch, we don’t have a way to reuse the animation
code that we already wrote because that only happens
when the app first starts. So now what we’re going to have to do is refactor that code into a new method, so our first step, I guess,
would be to create a new method. So let’s go ahead and do that. It’s going to be called
something like Add Labels. Now, you can pick whatever
name you want here. You don’t have to do exactly what I do, but add labels is pretty descriptive. It’s a verb. I know it’s going to do something, and now what we want
to do is we want to add both the iPhone label
and the My First label. But I don’t want to
take the creation line, so I’m going to go for the next line and just grab this code right here, and then I’ll use Edit,
Cut, to move it down here, so then I’ll paste it around line 52, wherever you added the add labels method. So now that we have that code, pause the video if you
didn’t grab this code, and you can put it down here. Next, we will come back up top, and right underneath this line, I’m going to go ahead and
grab the following line, so we’ll just cut this as well. Now, I’ll use the keyboard shortcut Command X to pull that out, and then down here we will insert that. So if all goes well, and
you copied everything that you needed to copy,
you should be good to go. If you didn’t, then we’re
going to have an issue. So if you were to run this right now nothing is going to happen because we haven’t told
it to do anything yet, and we took away all the logic
that did all the cool stuff, so we need to fix that, and the first thing that we’re going to do is we’re going to fix
the formatting up here. So this is where we create the labels. I’m just going to get rid
of the extra space here just to make it a little
bit more succinct, and when we do the tap gesture instead of printing a
message to the console that you can’t see when
you’re actually using the app, we’re going to make the labels appear. And so we can just call
the add labels method by saying add labels, open
parentheses, close parentheses, and that’s it, so let’s
go ahead and run this. And now if I click those
labels will animate on, and the cool thing is I can keep clicking and they will keep animating. Alright, so we learned to reuse code. The one problem that we had is that there was no animation
when we first started, so let’s fix that by adding
a call to add labels in here. Now, when we start it, if we
hit the play button up top, you will see that the animation starts, and now I can tap anywhere
and it will repeat, so that’s a little bit about
how to use some tap gestures in your application to make
an entire screen touchable, and that’s how to repeat an action that you want to repeat
by taking that code, refactoring it into a method that you can reuse somewhere else. So this is a common thing that you’ll see when you are creating an iPhone app, you’ll want to reuse
code in certain places, and so you’ll create these
chunks of code called methods, write this down in your notebook if you don’t know what that is, and functions, that’s
another good one to know. Both of those are very similar, but there is a difference. So if you search on Google
or you read the Swift book, you’ll be able to find
out the difference is. We’ll be learning more about these in the later course videos. Alright, so in the next course video I’m going to go over some common problems that you might have when you
are coping and pasting code or when you’re writing the selectors, or when things just aren’t working. So stay tuned and let’s
try to troubleshoot any issues that you might have right now.

2 Replies to “Swift 1.5 Tutorial – Add Tap Gestures to Animate Refactored Code from a Method”

Leave a Reply

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