Limit UITableView scroll speed like Instagram does it

后端 未结 2 503
轮回少年
轮回少年 2021-02-05 09:51

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 yo

相关标签:
2条回答
  • 2021-02-05 10:31

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

    0 讨论(0)
  • 2021-02-05 10:33

    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
    
    0 讨论(0)
提交回复
热议问题