views:

1477

answers:

2

Is there a way to deactivate the decelerating of a UIScrollView?

I want to allow the user to scroll the canvas, but I don't want that the canvas continues scrolling after the user lifted the finger.

+2  A: 

You can just turn up the deceleration rate very high. With an infinite rate, it would stop immediately. Try setting the rate to these constants:

scrollView.decelerationRate = UIScrollViewDecelerationRateNormal;

and

scrollView.decelerationRate = UIScrollViewDecelerationRateFast;

If fast still isn't fast enough for you, UIScrollViewDecelerationRateFast is just typedef'ed as a float, so you can just multiply it by a factor of 10 or so to speed it up even more.

Dan Lorenc
Thank you for your answer! I tried various values for the decelerationRate property, but didn't manage to make the deceleration appear non existence. A quick swipe always makes the scrollview scroll a considerable amount.
Markus Müller
+6  A: 

This can be done by utilizing the UIScrollView delegate method scrollViewWillBeginDecelerating to autmatically set the content offset to the current screen position.

To implement:

  1. Assign a delegate to your UIScrollView object if you have not already done so.
  2. In your delegates .m implementation file, add the following lines of code:

    -(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    [scrollView setContentOffset:scrollView.contentOffset animated:YES];
    }

Voila! No more auto-scroll.

Mark Hammonds
Nice. I have no idea why, but it works. Thank you!
Markus Müller