如何在iPhone中制作无限滚动视图? [英] How to make infinite scroll view in iPhone?

查看:90
本文介绍了如何在iPhone中制作无限滚动视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 scrollview 5张图片,宽度为88



我希望 scrollview 滚动到每个图片视图(Not Paging)





我想在iPhone中使无限滚动视图这意味着当它滚动到最后一个项目时它将显示它旁边的第一个项目..



我一直在尝试偏移但是它在移动时停止通过偏移和我使用了苹果街道滚动器,它不允许我停止中心的每个元素(就像Picker视图一样)..

解决方案

首先,我建议使用UITableView,这样可以保持较低的内存使用率。我在项目中成功使用的方法如下:

    1。列表项

复制单元格的内容,我的意思是如果你有这样的5项:



| 1 | 2 | 3 | 4 | 5 |



你应该在表的末尾添加相同的(在具有可重用引擎的表视图中,这不是什么大不了的事),看起来像这样: / p>

| 1 | 2 | 3 | 4 | 5 | 1 | 2 | 3 | 4 | 5 |

    2。使用以下内容修改scrollViewDidScroll:



   - (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{
if(scrollView == _yourScrollView){
CGFloat currentOffsetX = scrollView.contentOffset.x;
CGFloat currentOffSetY = scrollView.contentOffset.y;
CGFloat contentHeight = scrollView.contentSize.height;

if(currentOffSetY<(contentHeight / 6.0f)){
scrollView.contentOffset = CGPointMake(currentOffsetX,(currentOffSetY +(contentHeight / 2)));
}
if(currentOffSetY>((contentHeight * 4)/ 6.0f)){
scrollView.contentOffset = CGPointMake(currentOffsetX,(currentOffSetY - (contentHeight / 2)));
}
}
}

上面的代码移动滚动如果你几乎达到滚动的最后一个位置在顶部;或者,如果你几乎在顶部,将你移到底部...

    3。就是这样。


I have a scrollview with 5 image views of width 88.

I want the scrollview to scroll to each image view (Not Paging)

and

I want to make an infinite scrollview in iPhone which means the when it scroll to last item it will display a first item next to it..

I have been trying by offset but it stops when move it by offset and also I have used apple street scroller which does not allow me to stop each element in center(just like Picker view)..

解决方案

First of all, I recommend to use a UITableView so you can maintain the memory usage low. An approach that I had used successfully in a project is the following:

    1. List item

Duplicate the content of your cells, I mean if you have 5 items in this way:

| 1 | 2 | 3 | 4 | 5 |

You should add the same (In a table view with reusable engine that's not a big deal) at the end of the table in order to look like this:

| 1 | 2 | 3 | 4 | 5 | 1 | 2 | 3 | 4 | 5 |

    2. modify the scrollViewDidScroll with the following:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == _yourScrollView) {
        CGFloat currentOffsetX = scrollView.contentOffset.x;
        CGFloat currentOffSetY = scrollView.contentOffset.y;
        CGFloat contentHeight = scrollView.contentSize.height;

        if (currentOffSetY < (contentHeight / 6.0f)) {
            scrollView.contentOffset = CGPointMake(currentOffsetX,(currentOffSetY + (contentHeight/2)));
        }
        if (currentOffSetY > ((contentHeight * 4)/ 6.0f)) {
            scrollView.contentOffset = CGPointMake(currentOffsetX,(currentOffSetY - (contentHeight/2)));
        }
    }
}

The code above move the scroll position at top if you almost reach the final of the scrolling; Or if you are almost on the top, moves you to the bottom...

    3. That's it.

这篇关于如何在iPhone中制作无限滚动视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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