UIScrollView无限滚动 [英] UIScrollView Infinite Scrolling

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

问题描述

我正在尝试设置无限(水平)滚动的滚动视图。



向前滚动很容易 - 我已经实现了scrollViewDidScroll,当contentOffset接近时最后我让scrollview内容更大,并在空间中添加更多数据(我将不得不处理稍后会产生的严重影响!)



我的问题是滚动返回 - 计划是看到我何时接近滚动视图的开头,然后当我确实使内容更大时,移动现有内容,将新数据添加到开头然后 - 重要的是调整contentOffset以便数据在视图端口保持不变。



如果我慢慢滚动(或启用分页),但如果我快速(甚至不是非常快!)它会很生效!下面是代码:

   - (void)scrollViewDidScroll:(UIScrollView *)scrollView {

float pageNumber = scrollView.contentOffset.x / 320;
float pageCount = scrollView.contentSize.width / 320;

if(pageNumber> pageCount-4){
//添加10个新页面以结束
mainScrollView.contentSize = CGSizeMake(mainScrollView.contentSize.width + 3200,mainScrollView。 contentSize.height);
//在此处添加新数据(320 * pageCount,0);
}

// ***问题在这里 - 我使用updatedScrollingContent来确保它只调用一次(用于准确测试!)
if(pageNumber< 4& ;&!updatingScrollingContent){

updatingScrollingContent = YES;

mainScrollView.contentSize = CGSizeMake(mainScrollView.contentSize.width + 3200,mainScrollView.contentSize.height);
mainScrollView.contentOffset = CGPointMake(mainScrollView.contentOffset.x + 3200,0);
for([mainContainerView子视图]中的UIView *视图){
view.frame = CGRectMake(view.frame.origin.x + 3200,view.frame.origin.y,view.frame.size。 width,view.frame.size.height);
}
//在(0,0)处添加新数据;
}

// **我的检查!
NSLog(@%f,mainScrollView.contentOffset.x);
}

当滚动发生时,日志显示为:
1286.500000
1285.500000
1284.500000
1283.500000
1282.500000
1281.500000
1280.500000



然后,当pageNumber< 4(我们'接近开头):
4479.500000
4479.500000



太棒了! - 但数字应继续在4,000s下降,但下一个日志条目为:
1278.000000
1277.000000
1276.500000
1275.500000
等....



从它停止的地方继续!



只是为了记录,如果慢慢滚动,日志显示为:
1294.500000
1290.000000
1284.500000
1280.500000
4476.000000
4476.000000
4473.000000
4470.000000
4467.500000
4464.000000
4460.500000
4457.500000
等....



任何想法????



谢谢



Ben。

解决方案

可能是那个无论在那里设置这些数字,你设置contentOffset都不会给你留下太深刻的印象。所以它只是继续设置它认为应该是下一个瞬间的contentOffset - 而不验证contentOffset是否在此期间发生了变化。



我会将UIScrollView子类化并放入 setContentOffset 方法中的魔力。根据我的经验,所有内容偏移更改都会通过该方法,甚至是内部滚动引起的内容偏移更改。只需在某些时候执行 [super setContentOffset:..] 将消息传递给真正的UIScrollView。



也许如果你把你的换挡动作放在那里它会更好。您至少可以检测到contentOffset的3000关设置,并在传递消息之前修复它。如果您还要覆盖 contentOffset 方法,您可以尝试查看是否可以制作虚拟无限内容大小,并将其缩小到引擎盖下的实际比例。 / p>

I'm attempting to setup a scrollview with infinite (horizontal) scrolling.

Scrolling forward is easy - I have implemented scrollViewDidScroll, and when the contentOffset gets near the end I make the scrollview contentsize bigger and add more data into the space (i'll have to deal with the crippling effect this will have later!)

My problem is scrolling back - the plan is to see when I get near the beginning of the scroll view, then when I do make the contentsize bigger, move the existing content along, add the new data to the beginning and then - importantly adjust the contentOffset so the data under the view port stays the same.

This works perfectly if I scroll slowly (or enable paging) but if I go fast (not even very fast!) it goes mad! Heres the code:

- (void) scrollViewDidScroll:(UIScrollView *)scrollView {

    float pageNumber = scrollView.contentOffset.x / 320;
    float pageCount = scrollView.contentSize.width / 320;

    if (pageNumber > pageCount-4) {
        //Add 10 new pages to end
        mainScrollView.contentSize = CGSizeMake(mainScrollView.contentSize.width + 3200, mainScrollView.contentSize.height);
        //add new data here at (320*pageCount, 0);
    }

    //*** the problem is here - I use updatingScrollingContent to make sure its only called once (for accurate testing!)
    if (pageNumber < 4 && !updatingScrollingContent) {

        updatingScrollingContent = YES;

        mainScrollView.contentSize = CGSizeMake(mainScrollView.contentSize.width + 3200, mainScrollView.contentSize.height);
        mainScrollView.contentOffset = CGPointMake(mainScrollView.contentOffset.x + 3200, 0);
        for (UIView *view in [mainContainerView subviews]) {
            view.frame = CGRectMake(view.frame.origin.x+3200, view.frame.origin.y, view.frame.size.width, view.frame.size.height);
        }
        //add new data here at (0, 0);      
    }

    //** MY CHECK!
    NSLog(@"%f", mainScrollView.contentOffset.x);
}

As the scrolling happens the log reads: 1286.500000 1285.500000 1284.500000 1283.500000 1282.500000 1281.500000 1280.500000

Then, when pageNumber<4 (we're getting near the beginning): 4479.500000 4479.500000

Great! - but the numbers should continue to go down in the 4,000s but the next log entries read: 1278.000000 1277.000000 1276.500000 1275.500000 etc....

Continiuing from where it left off!

Just for the record, if scrolled slowly the log reads: 1294.500000 1290.000000 1284.500000 1280.500000 4476.000000 4476.000000 4473.000000 4470.000000 4467.500000 4464.000000 4460.500000 4457.500000 etc....

Any ideas????

Thanks

Ben.

解决方案

It could be that whatever is setting those numbers in there, is not greatly impressed by you setting the contentOffset under its hands. So it just goes on setting what it thinks should be the contentOffset for the next instant - without verifying if the contentOffset has changed in the meantime.

I would subclass UIScrollView and put the magic in the setContentOffset method. In my experience all content-offset changing passes through that method, even the content-offset changing induced by the internal scrolling. Just do [super setContentOffset:..] at some point to pass the message on to the real UIScrollView.

Maybe if you put your shifting action in there it will work better. You could at least detect the 3000-off setting of contentOffset, and fix it before passing the message on. If you would also override the contentOffset method, you could try and see if you can make a virtual infinite content size, and reduce that to real proportions "under the hood".

这篇关于UIScrollView无限滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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