像Instagram一样限制UITableView滚动速度 [英] Limit UITableView scroll speed like Instagram does it

查看:180
本文介绍了像Instagram一样限制UITableView滚动速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

如果你看看Instagram,你会注意到他们有限制滚动浏览量的速度。

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

由于限制不影响减速度,因此未使用decelerationRate设置。它只会影响您滚动Feed的速度。因此,如果您尝试执行轻弹手势,您将达到Instagrams最大滚动速度,并且不会像普通UITableView那样快。

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.

任何猜测Instagram如何实现这个目标?

Any guesses on how Instagram accomplishes this?

推荐答案

TableView有一个属性 scrollView ,这个属性将返回TableView的内部scrollView。使用以下...

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

tableview.scrollView.decelerationRate = UIScrollViewDecelerationRateFast;

另一种方式:

TableView将作出回应to scrollView委托,所以我们需要实现scrollView的委托,如:

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

取这些全局变量:

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

希望这可以帮助你......

Hope this may help you...

这篇关于像Instagram一样限制UITableView滚动速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆