Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reproduce the flick-to-scroll behavior of the iPhone UIScrollView in my own custom view?

I would like to reproduce UIScrollView's flick-to-scroll behavior, but I don't want to use (or can't use) that class. What can I do?

like image 648
Dave Peck Avatar asked Apr 24 '09 01:04

Dave Peck


1 Answers

Okay, I've answered this by implementing my own library that captures the dynamics of UIScrollView.

What’s useful about my code is that it is independent of coordinate system, animation rate, and particular UI classes that you're using. It 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.

like image 154
Dave Peck Avatar answered Oct 06 '22 05:10

Dave Peck