[MUSIC] Hello everyone. In the rest of this module, we're going to look at how to handle users' touches. As you can, as in the last lecture, we've implemented the scroll view. A scroll view essentially handles touches for you. It lets users do two things, pan, which makes the scroll view pan as well as zoom, which is really a pinching gesture as you can see. Sort of like when you do this, you're sort of pinching at the screen. Now, the duties, these are implemented through gesture recognizers. And by their name, it helps you recognize gestures and actually you can do all of this yourself without the help of the view and we're going to try that today. So a gesture recognizer is an object that you can actually add in the story board as well. For example, in the object library if you type gesture recognizer, you get all the kinds of recognizers you can add to your view. Let me enlarge this a bit. As you can see, there's a tab recognizer, pinch, which I was talking about for the zooming. Rotation gesture recognizer, which recognizes rotation. A swipe gesture recognizer. Pan, which is what is used for the panning of this global view. And the screen edge pan recognizer, which is an interesting one, which lets you recognize a pan originated from outside of the screen basically and is what you would use, what you would see being used for a sort of automatic back feature on navigation views. And also a long press recognizer, which recognizes when you tap and hold basically for a while. So today we are going to add the double tap to zoom feature to this app, to the scroll view. So we can scroll, we can zoom, but it would be cool if we could sort of double tap. And we'll double click since we're in a simulator, and have it zoom in. And so, that is actually easily handled by the tap gesture recognizer. So you drag the tap, the recognizer here by holding it. And you can add it into your view. And you can choose which view it gets attached to. So since we want this image view, it's the only we can add it to here. If you let go, it will get added here. And if you right click on it you'll see it's connected to the image view, just your recognizers, which is what you want. And you can connect multiple gesture recognizers to different places. And actually, that's not true. We don't want to add it to the image view. We want it to be on the scroll view. So you want to tap on the scroll view and you want to zoom. Instead of what's inside the scroll view. Which is image view, so we can get rid of that. You should be able to drag it to scroll view. Just recognizers. And it will replace that image view with the scroll view and it also can have a action, which is great. Which was what will be called when it is recognized. So we're going to go into the code here and we're going to press create an outlet for this. So Hold Option+R+Alt. Click on ViewController to open side by side. And what we're going to do here is control drag the tap to recognizer. And create an outlet for it, under filter button here. We're going to call it zoomTapGestureRecognizer. And maybe all the way down here to the bottom again, we'll add action for it, and again, just like a button, it works just like a button. Drag it over and call it on tap. Oops, that was incorrect. We forgot to make it a action instead of an outlet, so if you right click on here, we can delete on tap and do that again. Make sure it's an action, call it onTap, and it gets the TapGestureRecognizer as an argument, as the sender. So that's it for now. Let's build and run. Let's actually add a breakpoint here to me to see if this works. So once the app runs, you can do it on a device as well. I'm going to just click on the anywhere and we'll get this action. Cool, so you click on anywhere you want, and it doesn't interfere with your other features. Such as zooming and even panning. That's pretty interesting. That's because, you can handle this any way you want, the scroll view already has two gesture recognizers, and when it recognizes a pan, it'll automatically cancel the recognition of a tap, so you won't get this call here. But you can actually change that. You can make it so that a tap always does the tap, and you'll never be able to pan. But we should be happy right now that this is what we want. No, but actually, this is not exactly what we want. We want a double tap. So right now, it calls every time you tap once, instead of twice. And we're going to change that by scrolling up to. If you look, we're going to do this on viewDidLoad. because we only have to do this once. A gesture recognizer has everything we need, has lots of options. And one very convenient one is number of taps required. Then this one, of course, only exists on the tab gesture recognizer. And we're going to change that to two. Run again. And now if you tap once nothing happens. I'm tapping here and you can trust me. But if I double tap, it triggers. And this is also very smart, like it's not going to just, if I tap, double tap very slowly. You won't trigger either. So it's exactly what we want. It's basically number of taps, you change it to two, and that's what it will do. Interestingly, you can change that to as much as you want. You can make it like a five tap recognizer, which might be useful in some cases. I try not to make users ever try to use that. All right, so last thing I'm going to do here is just increase our zoom. This will be quite, real interesting. We don't have the outlet for our ScrollView yet, just realized. because we added that last time we didn't need one. So we're going to just add that. Got the secondary menu inside W controller. Grab my scroll view. And Cmd+C that. You want to drag the scroll view here. We're going to call it our scrollView. Back down to our function. All we're going to do is change the zoomScale on the scrollView and increase it by 1.5. And this is going to be very rough, but let's see what happens. Hey, not too bad. Can scroll in. I think it would only scroll, it scrolls in towards the center. Not too bad, with some more code, some more logic you can easily make it zoom in to where you're tapping. Even, this is probably animatable if you still remember. How to do that. There's a animateWithDuration on UI view. And we're going to pick the simplest one, animateWithDuration. Stays there 0.4 seconds. And we're just going to drop this in there. And we'll have a nice animated zooming, except that since it's in a closure now, we have to reference with the self. And the compiler will happily tell you to do that. Nice. All right, so that's it for this lecture. You can check out the other gesture recognizers, they're really simple to use, you can add them to any view you want. And in the next lecture we'll come back and see how to do this without gesture recognizers.