Add Swipe Gestures for Mobile Apps on Developer

show more Adding swipe gestures for mobile provides you with in-depth training on Developer. Taught by Justin Putney as part of the HTML5 Projects: Creating a Responsive Presentation show less
please wait ...

Adding swipe gestures for mobile

The cool things about creating your own responsive presentation is that not only can you take advantage of things that the user already expects, like using the arrow keys that they'd find in a PowerPoint presentation, but scaling down to devices, you can take advantage of other expectations like swipe gestures. So, let's go ahead and add a swipe library and wire up the swipe gestures to the navigation we already have. Let's start by going into the chapter 3 folder, 03_03, start folder, js, and open the pres.js file.

We're going to be adding more code inside a ready function to handle the swipe, but first let's download the library and make sure that library is incorporated in our project. There's a couple different libraries you can use. The one w're going to use in this case is TouchSwipe. If you don't have access to the exercise files, just do a search for jQuery and then TouchSwipe, all one word. And we want this minimize, this minified file. If you do have access to the exercise files, you can grab a copy from this JS folder.

I'm just going to select that, copy it and paste it into the JS folder that we are actively working on. So we're going to then reference this minified file. I'm just going to go back to the Finder here and open the index.html file in Dreamweaver. We want to add another script tag. We want to make sure that it is available before our pres.'s file, so let's add it after jQuery, because it also relies on jQuery. Add the script tag.

I'm going to set the source. And let's go ahead and just browse for the source and select that jQuery library. Click Open. There it is. Let's close the tag and save the index.html file. I'm going to switch over to pres. js and let's start adding this code. Now, this code is kind of a long chunk and it's a little bit complicated, so if you're worried about making mistakes or you'd rather just copy and paste it, you can grab it from the snippets file in this video's exercise folder.

So let's call the jQuery function again. This time we're going to look for that slides containers so we want to detect any swipes that happen on the container that holds all the slides. And the syntax for this is that you then call this swipe function and then we pass in the parameters that we're going to use. So this TouchSwipe library is rather advanced. There's a lot of features in it that you may want to play with on your own, but for right now we're just going to do a real simple implementation.

I'm going to open up this object with the curly bracket and for this swipe property, it's going to be a function. And we're going to type out all the parameters from the documentation, but we're only going to use one of them, for right now. You can of course take advantage of more of them on our own. The first one is an event and then direction, distance--these are separated by commas--and duration, and fingerCount. And then we're going to close that parenthesis and open curly bracket.

And I'm just going to hit another line to return, close the curly bracket, and I'm going to close the other curly bracket and also close this parentheses here, and end line with a semicolon. So here we want to detect for what direction the user has swiped, so we're going to add a switch statement. And again you can use if- else if you prefer with this. It's really up to you. So we want to switch on that direction.

We're going to add two cases. You can look for up-down if you'd like. In this case let's just look for left and right. So I've got case left and then a colon. I'm gonna tab in. So if that's the case, let's go ahead and call that next line. And then we're going to break after that so no other code gets executed, and we're going to go back a tab and write a second case. This one's going to be to the right, and a colon. Then I'm going to tab in and say back if the user happens to swipe the right, and we're going to break again and we should be all.

It looks good like I've got a closing bracket there that Dreamweaver added for me, so that should be all in place. So this might seem a bit counterintuitive that our swipe gestures effectively work in the opposite direction of our key events. And the reason that is is because when I say, if I click to the right of fingerCount here and I drag to the left, the motion--you'd think of it as maybe effectively throwing the current screen away. So in a left-to-right culture of reading English, fingerCount comes after direction for instance.

So as I'm pushing things forward, suppose this selection is my viewfinder, fingerCount comes into view, and that's a later item than, say, direction. So as I swipe to the left, I'm moving forward. If I swipe to the right, I'm kind of going back in space. So it's a little bit counterintuitive, but something to think about. And of course if you add up or down, you're going to find the same thing there. Switch back to the Finder and open up the corresponding index.html file in my browser.

And now in addition to the arrow keys working, if I click and drag, I should be able to swipe to the next screen. There we go. And it works if I go backwards too. All right. We're going a long way towards having a responsive presentation, in that we are dealing with the medium of the touchscreen. In the next chapter we're to modify the CSS to also take advantage of the different options on different screens.

Adding swipe gestures for mobile
Video duration: 5m 58s 1h 25m Intermediate


Adding swipe gestures for mobile provides you with in-depth training on Developer. Taught by Justin Putney as part of the HTML5 Projects: Creating a Responsive Presentation

Developer Web
please wait ...