本文介绍了iPhone UIScrollView速度检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何在UIScrollView上运行contentOffset,有人可以向我解释如何在跟踪或减速时获得表示UIScrollView当前速度的实际数字吗?

I know how to get the contentOffset on movement for a UIScrollView, can someone explain to me how I can get an actual number that represents the current speed of a UIScrollView while it is tracking, or decelerating?

推荐答案

在UIScrollViewDelegate上拥有这些属性

Have these properties on your UIScrollViewDelegate

CGPoint lastOffset;
NSTimeInterval lastOffsetCapture;
BOOL isScrollingFast;

然后将此代码用于scrollViewDidScroll:

Then have this code for your 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;
    }
}

从这里我得到每毫秒像素,如果大于0.5,我的记录速度很快,下面的任何内容都记录为慢。

And from this i'm getting pixels per millisecond, which if is greater than 0.5, i've logged as fast, and anything below is logged as slow.

我用它来加载动态表视图上的一些单元格。如果我在用户快速滚动时加载它们,它就不会滚动得那么好。

I use this for loading some cells on a table view animated. It doesn't scroll so well if I load them when the user is scrolling fast.

这篇关于iPhone UIScrollView速度检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 05:00