no way to compare when less than two revisions

Differences

This shows you the differences between two versions of the page.


Previous revision
tutorials:intertial_scrolling [2013/03/05 10:19] (current) – external edit 127.0.0.1
Line 1: Line 1:
 +====== Inertial Scrolling ======
  
 +On the iOS, scrolling works in way that feels very natural: when you move, say, a page in Safari, it doesn't stop moving immediately when you lift the finger off the screen. Instead, the page moves on and gets slower over time, coming to a full halt after about a second.
 +
 +Here is a code snippet that allows you to move objects in a similar way.
 +
 +<code objc>
 +// in the init-method: (add the variables as member variables to your class!)
 +mTouching = NO;         // BOOL
 +mLastScrollDist = 0.0f; // float
 +mList = [[MyCoolList alloc] init];  // that is the object you'd want to move
 +[self addChild:mList];
 +[mList addEventListener:@selector(onTouch:) atObject:self
 +       forType:SP_EVENT_TYPE_TOUCH];
 + 
 +// touch event listener:
 +- (void)onTouch:(SPTouchEvent*)touchEvent
 +{
 +    mTouching = [[touchEvent touchesWithTarget:self andPhase:SPTouchPhaseEnded] count] == 0;
 +    if (!mTouching) return;
 + 
 +    SPTouch* touch = [[touchEvent touchesWithTarget:self andPhase:SPTouchPhaseMoved] anyObject];
 +    SPPoint *localPos = [touch locationInSpace:self];
 +    SPPoint *previousLocalPos = [touch previousLocationInSpace:self];
 + 
 +    mLastScrollDist = previousLocalPos.y - localPos.y;
 +    mList.y += mLastScrollDist;
 +}
 + 
 +// enter frame event listener
 +- (void)onEnterFrame:(SPEnterFrameEvent *)event
 +{
 +    if (!mTouching)
 +    {
 +        float slowDown = 0.98f;
 + 
 +        if (fabsf(mLastScrollDist) < 0.5f)
 +            slowDown = 0;
 + 
 +        mLastScrollDist *= slowDown;
 +        mList.y += mLastScrollDist;
 +    }
 +}
 +</code>
 +
 +Beware that this is not a perfect implementation; i.e. it behaves differently depending on the frame rate. But it should be a good place to start. More information can be found [[http://forum.sparrow-framework.org/topic/inertial-scrolling|in this forum thread]].
  tutorials/intertial_scrolling.txt · Last modified: 2013/03/05 10:19 by 127.0.0.1
 
Powered by DokuWiki