Deactivate UIScrollView decelerating
Asked Answered
K

4

40

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.

Karriekarry answered 9/7, 2009 at 18:17 Comment(2)
Adam, I'm aware that changing the default behavior isn't recommend but I think in my special case it would make sense. I'm implementing an extended canvas with the ability to edit objects directly on the canvas. When entering this on-canvas-edit mode I would like to restrict the distance the user scrolls so that the object is alway in the visible range. The use is allowed to scroll further, but it would bounce back on finger up. I'm aware of the contentSize property, but it interferes with my growing canvas.Rosalba
In my situation, we were doing paid for hire work and the client requested that we remove the deceleration motion.Wright
W
72

This can be done by utilizing the UIScrollView delegate method scrollViewWillBeginDecelerating to automatically 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 delegate's .m implementation file, add the following lines of code:

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

Voila! No more auto-scroll.

Wright answered 6/8, 2009 at 21:8 Comment(4)
Mine jumps to where the deceleration would have ended back to where it started... Any suggestions?Ezarra
I found using this method will interfere with touch events in the view being zoomed. (In my case I had some buttons in the view that became inactive when zoomed out.) Using Quotation's fix below fixed the issue.Sumer
This does seem to work but i am hesitant to use something that no-one can seem to explain. Mark could you clarify as to why this works?Latinize
Thomas Clowes: The above solution was written long ago and is something of a hack. When the scroll view deceleration animation begins, it calls the delegate method scrollViewWillBeginDecelerating, and my one line of code simply manually forces the content offset (think of this as the visible portion of the scroll view) to stay at whatever position it was already at, forcing no visual change. These days, I'd recommend using the delegate method pointed out below by Quotation. Set the targetContentOffset to the current offset to prevent motion.Wright
C
44

For iOS 5.0 or later, there is a better method than calling setContentOffset:animated:.

Implement delegate method scrollViewWillEndDragging:withVelocity:targetContentOffset: in your .m file:

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
                     withVelocity:(CGPoint)velocity
              targetContentOffset:(inout CGPoint *)targetContentOffset {
    targetContentOffset.pointee = scrollView.contentOffset;
}

Assigning the current offset to targetContentOffset stops the UIScrollView from auto-scrolling.

Camphor answered 17/10, 2012 at 5:49 Comment(0)
W
21

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.

Wileywilfong answered 9/7, 2009 at 18:22 Comment(4)
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.Rosalba
In my experience, the decelerationRate property only responds to the values explicated in the documentation: UIScrollViewDecelerationRateNormal and UIScrollViewDecelerationRateFast.Woodworking
This is great, as it still enables the content to not go past the desired bounds. When implementing the other options, I could pan the content farther than desired.Sharrisharron
If you want to prevent that from happening set scrollView.bounces = falsePlumose
S
1

Just set the decelerationRate property to 0

It will disable the auto scrolling property. But keep in mind the user interaction will become bad if scrollview contentsize is big.

Subdebutante answered 28/3, 2018 at 7:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.