Limit UITableView scroll speed like Instagram does

2019-03-14 16:07发布

问题:

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?

回答1:

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...



回答2:

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