views:

1224

answers:

5

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?

+5  A: 

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.

Dave Peck
You answered your own question -- with some pretty involved code -- within 2 minutes of asking it? It's interesting code so OK!
Daniel Dickison
Yeah I didn't know if that was kosher for Stack Overflow but since I discovered I'm not the only one looking for it, I figured it would be helpful? Is there a better forum?
Dave Peck
A: 

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

Kendall Helmstetter Gelner
I have an OpenGL view for a game and I wanted to be able to control the viewport in a manner similar to that of UIScrollView...
Dave Peck
A: 

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 :-)

Roger Nolan
See my comment above. I am trying to reproduce the behavior in a context where UIScrollView can't go (or, at least, shouldn't go.)
Dave Peck
(And: I should point out that what I did is NOT the same as just setting a few properties on a scroll view. If that's what I had needed to do -- well, I would have done so. :-)
Dave Peck
...and I'm interested what that is ;-)
Roger Nolan
I tried to hint at it in my answer above but I guess you want more detail? In my case, scrolling takes place over an arbitrary plane in my OpenGL world. That is: what to FlickDynamics.m looks like simple x/y is to my calling code actually coordinates on a plane of some known orientation for which I have a direct orthographic projection...
Dave Peck
The reason I went to all this trouble is that my situation didn't seem like the only one. I can think of several other unique answers to your question that are all perfectly legitimate and much simpler than my actual answer. For example, "I want to use a different gesture than what UIScrollView accepts by default because my app reserves single-finger-drag for something else" or "I need to sync my scrolling animation with my game's frame rate." Hope that helps set the context! :-)
Dave Peck
A: 

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.

Lee Ann Rucker
A: 

Could you help me with using your FlickDynamics code(any sample)? I'm newbie in iPhone programming and need to implement UIScroll-like behavior in my game which using OpenGL for drawing. Thanks )

andrey.s