限制UITableView的滚动速度,就像Instagram一样



我试图限制我的UITableView的滚动速度,就像Instagram一样。

如果您查看Instagram,您会注意到他们对滚动浏览提要的速度有限制。

它不是使用"减速速率"设置的,因为限制不会影响减速。它只会影响您滚动浏览提要的速度。因此,如果您尝试做"轻拂"手势,您将达到Instagram的最大滚动速度,并且不会像普通UITableView那样快。

关于Instagram如何实现这一目标的任何猜测?

TableView

有一个属性scrollView,此属性会返回 TableView 的内部滚动视图。使用以下...

tableview.scrollView.decelerationRate = UIScrollViewDecelerationRateFast;

另一种方式:

TableView 将响应 scrollView 委托,因此我们需要实现 scrollView 的委托,如下所示:

取这些全局变量:

CGPoint lastOffset;
NSTimeInterval lastOffsetCapture;
BOOL isScrollingFast;

实现如下scrollViewDidScroll

- (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;
    }
}

然后实现如下scrollViewDidEndDragging

- (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];
    }
}

希望这可以帮助您...

使用这个:

self.tableview.scrollView.decelerationRate = UIScrollViewDecelerationRateFast;

由于tableView是UIScrollView的子类,因此ScrollView委托将在此处工作。希望这有帮助..:)

编辑:

如果表视图未显示滚动视图属性使用:

self.tableView.decelerationRate

在 Swift 中像这样设置

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

UIScrollView.减速率有两种速率类型正常和快速。默认情况下,其正常值(大约值>= 0.9)。没有检查快速值。

要像Instagram一样,您需要使用scrollViewWillEndDragging方法检查速度。

检查速度,如果高于某个阈值,则根据需要设置减速率。

相关内容

最新更新