iPhone UIScrollView 速度检查 [英] iPhone UIScrollView Speed Check

查看:12
本文介绍了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 速度检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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