Bright Hub
 
Matthew Casperson's Hubfolio

iPhone kinetic scrolling in Adobe Flash

Article by Matthew Casperson (227,413 pts )
Published on Nov 15, 2009

This free tutorial shows you how to add the same kinetic scrolling found in the iPhone to Adobe Flex.

Introduction

VIEW THE DEMO

DOWNLOAD THE SOURCE CODE

The iPhone touch screen interface includes a number of user friendly controls that respond well to tactile input. One of these is "kinetic" scrolling, where the movement of the screen follows a natural acceleration/deceleration flow, just like a piece of paper sliding across the desk. This tutorial will show you how to replicate this effect in Adobe Flex.

Adobe Flex iPhone kinetic scrolling screenshot

Credits

The code for the iPhone scrolling originally comes from the Flex component that can be found on the Adobe website here. I have modified the code slightly to make it easier to access the scrolling speed properties. The new code is included with the source code download.

Step 1 - Define the interface

The MXML markup below creates the user interface. We first create a Canvas element, and then add an image inside it. By placing a large Image in a small Canvas we can create a section of the interface that can be scrolled. On top of that we add some Sliders that allow us to modify the kinetic scrolling speed.

<mx:Canvas id="imageCanvas" left="10" top="10" right="10" bottom="10">

<mx:Image source="@Embed('../media/image1.jpg')" scaleContent="false"/>

</mx:Canvas>

<mx:Label x="10" y="10" text="Acceleration" color="#FFFFFF"/>

<mx:HSlider x="89" y="10" minimum="1" maximum="50" change="{manager.factorAcceleration = acceleration.value;}" id="acceleration" snapInterval="1"/>

<mx:HSlider x="89" y="36" minimum="1" maximum="50" change="{manager.factorDesacceleration = deceleration.value;}" snapInterval="1" id="deceleration"/>

<mx:Label x="10" y="36" text="Deceleration" color="#FFFFFF"/>

Step 2 - The code entry point

Add the following attribute to the Application element. This sets the appComplete function as our ActionScript code entry point.

applicationComplete="appComplete()"

Step 3 - Add the kinetic scrolling

The kinetic scrolling is implemented by the IPhoneScrollerManager class. By supplying a container to the addContainer function we are creating a new IPhoneScrollerManager object that will take care of all the mouse interaction and scrolling in the background. The addContainer function returns the new IPhoneScrollerManager object (this is a change from the original code), ad we can then use the properties of that object to modify the speed of the scrolling.

For consistency, the values displayed by the sliders in step 1 are updated to reflect the default values of the IPhoneScrollerManager class.

<mx:Script>

<![CDATA[

private var manager:IPhoneScrollerManager;

private function appComplete():void

{

manager = IPhoneScrollerManager.addContainer(imageCanvas);

this.acceleration.value = manager.factorAcceleration;

this.deceleration.value = manager.factorDesacceleration;

}

]]>

</mx:Script>

Conclusion

The iPhone scrolling method is natural to use, and may even be preferable for Flex and Flash applications where screen space is limited (like banners and widgets). The IPhoneScrollerManager class makes implementing this style of scrolling trivial, and using this demo you can easily find a scrolling speed that suits your application.

Return to the Tutorial Index

Related Article

ipodSpark Project: LCDBitmap

Learn how to use the LCDBitmap class to produce a stylised, low resolution video player.

Search More About:

Comments

Jul 5, 2010 9:03 AM
Ben
dragging doesn't work
Hey Matthew, the integration was perfect and it still works.

But if i you are still dragging it while holding the mouse down it gets acceleration and decceleration and of course is changing the direction but it doesn't follows the Cursor.

It should stick on the cursor.

And what about these smooth pulling and backflipping effect on the border of the canvas?
Jun 30, 2010 2:17 PM
Vadim Kacherov
Lists
Good job Mat. I've got the app working with Containers, but would like to use it on a HorizontalList. I tried putting the List inside a Container, but the Container "sees" the List as 100% and therefore will not scroll. Any idea how to do that? Thanks.
May 25, 2010 2:40 PM
Yonaz
Nice
Is it possible to adapt this to the ScrollPane on Flash CS5?
Mar 29, 2010 3:24 AM
adicahya
RE: iPhone kinetic scrolling in Adobe Flash
hi,

im not an web professional, but, like to use your script to show my design internally.

im completely lost.
how to i embed this code inside a standard html page.

please advice

thx
Jan 15, 2010 11:04 AM
Raghavendra
great work
I have never seen this kind of work beauty .... keep going...


Raghavendra K
http://raghavendra.macoscoders.com
 
Follow Matthew Casperson
Receive weekly updates from Matthew Casperson
 
Bright Hub - Science & Technology Articles, Buyer's Guides, How-To Tips and Software Reviews
About Bright Hub | Contact Us | Advertise with Us | Become a Writer | RSS | Site Map | Terms of Use | Privacy Policy | Copyright Policy
©2010 Bright Hub Inc. All rights reserved. Page copy protected against web site content infringement by Copyscape