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

StackOverflow https://stackoverflow.com/questions/784192

Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

So what about UIScrollView did not work for you? That class is pretty flexible...

Dave, I see you've answerd your own question with some code but, doesn't setting the following give you what you need:

myScrollView.bounces = YES;
myScrollView.pagingEnabled = YES;
myScrollView.directionalLockEnabled = YES;

Specifically, it sounds like you have reimplemented directionLockEnabled. Maybe understanding why you can't use UIScrollView is the more interesting problem :-)

This is cool! I've used it in an app where my scrollable view is one day's worth of graphical data in a range from 1902-2037, so a UIScrollView would not be efficient.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top