18

I'm trying to limit the scroll speed of my UITableView, exactly like Instagram does it.

If you check out Instagram, you'll notice that they have a limit on how fast you can scroll through the feed.

It's not set using "decelerationRate" since the limit doesn't affect the deceleration. It simply affects the how fast you can scroll through the feed. So if you try to do a "flick" gesture, you will hit Instagrams max scrolling speed and won't go as fast as in a normal UITableView.

Any guesses on how Instagram accomplishes this?

8
  • 2
    Why do you think Instagram is doing something special? I don't experience any scrolling speed limit when I try to reproduce.
    – thelaws
    Commented Jun 19, 2015 at 20:42
  • @thelaws it most definitely scrolls slower somehow. When you scroll through the feed like you normally would and try and scroll through another feed in another app, then Instagram seems much more smooth. Commented Jun 19, 2015 at 22:09
  • 1
    Looks like it is very smooth, but not slow at all.
    – k06a
    Commented Jun 20, 2015 at 21:13
  • @k06a Smooth then... I would still say it's a little slower. But I guess the general feel you get, is that it's smooth. How do they do that? Commented Jun 21, 2015 at 4:42
  • 1
    I agree that it's not slowed down at all but I assume by 'smooth', you mean that there's no feeling of skipped frames while scrolling. If that's the case, then it's likely an issue of moving as much loading logic as you can to a background thread to allow the main thread to render as close to 60fps as possible. Instagram has a relatively simple table cell setup (one image, some text, couple of buttons). If you're getting a 'choppy' feeling while scrolling, I suggest doing a search for how to load images and other things in a background thread.
    – JJC
    Commented Jun 22, 2015 at 19:15

3 Answers 3

15

TableView has a property scrollView, This property will return internal scrollView of TableView. Use following...

tableview.scrollView.decelerationRate = UIScrollViewDecelerationRateFast;

ANOTHER WAY:

TableView will respond to scrollView delegate, so we need to implement scrollView's delegate like:

Take these global variables :

CGPoint lastOffset;
NSTimeInterval lastOffsetCapture;
BOOL isScrollingFast;

Implement scrollViewDidScroll like :

- (void) scrollViewDidScroll:(UIScrollView *)scrollView {    
    CGPoint currentOffset = scrollView.contentOffset;
    NSTimeInterval currentTime = [NSDate timeIntervalSinceReferenceDate];

    NSTimeInterval timeDiff = currentTime - lastOffsetCapture;
    if(timeDiff > 0.1) {
        CGFloat distance = currentOffset.y - lastOffset.y;
        //The multiply by 10, / 1000 isn't really necessary.......
        CGFloat scrollSpeedNotAbs = (distance * 10) / 1000; //in pixels per millisecond

        CGFloat scrollSpeed = fabsf(scrollSpeedNotAbs);
        if (scrollSpeed > 0.5) {
            isScrollingFast = YES;
            NSLog(@"Fast");
        } else {
            isScrollingFast = NO;
            NSLog(@"Slow");
        }        

        lastOffset = currentOffset;
        lastOffsetCapture = currentTime;
    }
}

Then implement scrollViewDidEndDragging like this :

- (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if( !decelerate )
    {
        NSUInteger currentIndex = (NSUInteger)(scrollView.contentOffset.x / scrollView.bounds.size.width);

        [scrollView setContentOffset:CGPointMake(scrollView.bounds.size.width * currentIndex, 0) animated:YES];
    }
}

Hope this may help you...

6
  • Thanks for the answer. I'm quite sure it won't work though. "decelerationRate" just controls the deceleration, so that definitely won't work. Setting the point to scroll manually using an animation won't work either. The user don't have a feeling of decelerating when it's simply an animation. Any other options? Commented Jun 11, 2015 at 5:05
  • Try to look at instagram and try to flick the main feed and you'll see what I'm after. Commented Jun 11, 2015 at 5:07
  • Just try both options, I have used second option and it is working for me very fine. It would take just 5 minutes to try this option. Commented Jun 11, 2015 at 5:11
  • Can you post this in Swift. I am in an urgent need of the solution.
    – G.Abhisek
    Commented Feb 26, 2016 at 14:21
  • @NiravGadhiya scrollViewDidScroll only this method for fast scrolling ? Commented Jun 25, 2016 at 9:32
9

Use this:

self.tableview.scrollView.decelerationRate = UIScrollViewDecelerationRateFast;

As tableView is a subclass of UIScrollView, ScrollView delegate will work here. Hope this helps.. :)

Edit:

if tableView doesn't show scrollView property use:

self.tableView.decelerationRate
1
  • Thanks for the answer. "decelerationRate" simply controls the deceleration rate and has nothing to do with the general speed, so that won't work. Check out Instagram and try to flick the main feed and you'll see what I'm after. Commented Jun 11, 2015 at 5:06
0

In Swift set like this

tableView.decelerationRate =  UIScrollView.DecelerationRate(rawValue: 0.5)

UIScrollView.DecelerationRate having two rate types normal and fast. By default its normal (approx value >= 0.9). didn't check about fast value.

To do like Instagram you need to check velocity using scrollViewWillEndDragging method.

check velocity, if its above some threshold then set decelerationRate as per your need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.