iPhone Scrolling Dynamics

I’ve written some code that reproduces the flick-to-scroll dynamics that you typically find on the iPhone. What’s useful about this code is that it is independent of coordinate system and can easily be nested in a custom view class of your choosing.

The iPhone’s default flick-to-scroll behavior is interesting. If you hold your finger down for a long time and move it in a variety of directions, you’ll see that only the very last direction is used to compute the scrolling motion.

If you try to build this code yourself, however, you’ll quickly discover that simply using the last two points to compute the direction of motion isn’t going to cut it. Instead, my code keeps a short history of touches and uses linear interpolation to determine where the touch “would have been” some small amount of time ago. This interpolated point is used as the basis for computing the motion vector. This leads to a very pleasing interaction.

The code is available on github, here: http://gist.github.com/100855. It is released under the BSD license.