如何在iOS中使uiscrollview无限?

时间:2013-10-09 09:43:34

标签: iphone objective-c ipad uiscrollview ios7

我想像那样滚动1 2 3 1 2 3

我有一些按钮假设10我想在无尽的卷轴上显示。

 numbercolors=[[NSMutableArray alloc] init];

 //total count of array is  49 

 numbercolors = [NSMutableArray arrayWithObjects:@"25",@"26",@"27",@"28",@"29",@"31",@"32",@"33",@"34",@"35", @"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"24",@"25",@"26",@"27",@"28",@"29",@"30",@"31",@"32",@"33",@"34",@"35", @"0",@"1",@"2",@"3",nil];

  int x=2500;

for (NSInteger index = 0; index < [numbercolors count]; index++)
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(x ,0,29.0,77.0);

    button.tag = index;

    [button setTitle:[numbercolors objectAtIndex:index] forState:UIControlStateNormal];

    [button addTarget:self action:@selector(didTapButton:) 

    forControlEvents:UIControlEventTouchUpInside];

    [coloringScroll addSubview:button];

    x=x+70+29;
}
 [coloringScroll setContentSize:CGSizeMake(5000+ (29+70)*[numbercolors count], 1)];

 [coloringScroll setContentOffset:CGPointMake(2500+(29+70)*11, 0)];

这是我在scrollview上制作butttons的代码。

如何设置 - (void)scrollViewDidEndDecelerating :( UIScrollView *)发送此方法进行无限滚动。

8 个答案:

答案 0 :(得分:8)

只需要设置setContentOffset count

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x > 2500+(29+70)*4 + ((29+70)*36)) {
        [scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x-((29+70)*36),  0)];  
    }
    if (scrollView.contentOffset.x < 2500+(29+70)*4){
       [scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x+((29+70)*36), 0)];
    }
}

答案 1 :(得分:7)

主要思想是将滚动视图重新定位回某个常量值,当滚动到某个值时,然后自动重新定位“1”,“2”,“3”项目。

例如 - 您将拥有contentwidth with contentwidth 5000.默认情况下,您将其设置为位置2500。

然后您只需登记- (void)scrollViewDidCScroll:(UIScrollView *)scrollview - 如果scrollview.contentoffset.x&gt; 3500 - 然后将它的位置减小到scrollview.contentoffset.x - = 1000;

另一方面也是如此。这将导致无限滚动。

但内容不会随之而来。所以 - 您需要实现额外的内容偏移值检查以重新排序并正确地重新定位“1”,“2”,“3”,项目。

我通常使用3个元素,然后为它们动态预加载必要的图库图像。

如果您不想重新发明“轮子”,请查看以下解决方案:

https://www.cocoacontrols.com/controls/dmcircularscrollview

https://www.cocoacontrols.com/controls/infinitescrollview

https://www.cocoacontrols.com/controls/iainfinitegridview

答案 2 :(得分:3)

您可以根据Apple StreetScroller iOS示例代码查看此解决方案:

https://github.com/gblancogarcia/GBInfiniteScrollView

我希望它有所帮助!

答案 3 :(得分:2)

我编写了一个无限滚动的UIscrollView子类来完成这个任务。它基于Apple StreetScroller示例。您只需提供按钮标题,以及是否需要水平或垂直滚动​​。您还可以设置许多其他选项。它在我的github上发布了文档和示例项目。这是链接...

http://github.com/ninefifteen/SSRollingButtonScrollView

答案 4 :(得分:1)

这是一个老问题,但我写这篇文章是为了帮助搜索相同解决方案的人。假设我们希望无限循环遍历3个项目(单元格) - C0,C1,C2,我们可以在中心单元格的左侧和右侧生成虚拟单元格,结果如下,

C0 C1 C2 [C0 C1 C2] C0 C1 C2

括号中的单元格是我们通过设备屏幕看到的单元格,如果我们向左滚动,

C0 [C1 C2 C0] C1 C2 C0 C1 C2

此时,强制contentOffset指向给定虚拟单元格的右侧,

C0 [C1 C2 C0] C1 C2 C0 C1 C2 - &gt; C0 C1 C2 C0 [C1 C2 C0] C1 C2

由于无缝地实现这一工作非常耗时,我推荐以下解决方案。

https://github.com/DragonCherry/HFSwipeView

如果您只是想查看它是如何工作的,请点击下面的链接然后“点按即可播放”。

https://www.cocoacontrols.com/controls/hfswipeview

答案 5 :(得分:0)

我在swift中实现了一个无限卷轴 它的灵感来自于苹果StreetScroller示例

我知道还不完美,但我认为这是一个神的起点。

<强> InfiniteVerticalScroller

答案 6 :(得分:0)

对于右无限滚动,您可以使用-

        let rightOffset = CGPoint.init(x: (scrollView.contentSize.width) - (scrollView.bounds.width), y: 0)
    UIView.animate(withDuration: 3.0,
                   delay: 0.0,
                   options: [.curveLinear, .repeat],
                   animations: { () -> Void in
                    self.scrollView.contentOffset = rightOffset

    }, completion: { (finished: Bool) -> Void in
        self.scrollView.setContentOffset(.init(x: 0, y: self.scrollView.contentOffset.y), animated: false)
    })

答案 7 :(得分:0)

这里提供的答案很好,但是如果您仍然不满意并且正在寻找其他方法,这就是我所做的。

主要思想是在您到达最后一个单元格之前的任何时间更新单元格数量。每次将项目数增加1时,都会产生无限滚动的错觉。为此,我们可以利用scrollViewDidEndDecelerating(_ scrollView: UIScrollView)函数来检测用户何时完成滚动,然后更新集合视图中的项目数。这是实现此目的的代码段:

class InfiniteCarouselView: UICollectionView {

    var data: [Any] = []

    private var currentIndex: Int?
    private var currentMaxItemsCount: Int = 0
    // Set up data source and delegate
}

extension InfiniteCarouselView: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // Set the current maximum to a number above the maximum count by 1
        currentMaxItemsCount = max(((currentIndex ?? 0) + 1), data.count) + 1
        return currentMaxItemsCount

    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
        let row = indexPath.row % data.count
        let item = data[row]
        // Setup cell
        return cell
    }
}

extension InfiniteCarouselView: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.frame.width, height: collectionView.frame.height)
    }

    // Detect when the collection view has finished scrolling to increase the number of items in the collection view
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        // Get the current index. Note that the current index calculation will keep changing because the collection view is expanding its content size based on the number of items (currentMaxItemsCount)
        currentIndex = Int(scrollView.contentOffset.x/scrollView.contentSize.width * CGFloat(currentMaxItemsCount))
        // Reload the collection view to get the new number of items
        reloadData()
    }
}

优点

  • 直接实施
  • 不使用Int.max(我个人认为这不是一个好主意)
  • 不使用任意数字(例如50之类的数字)
  • 不更改或处理数据
  • 不能手动更新内容偏移量或任何其他滚动视图属性

缺点

  • 应启用分页(尽管可以更新逻辑以不支持分页)
  • 需要维护某些属性(当前索引,当前最大计数)的引用
  • 需要在每个滚动端重新加载集合视图(如果可见单元格很小,则没什么大不了的)。如果您在不缓存的情况下异步加载某些内容,则这可能会严重影响您(这是一种不良做法,应将数据缓存在单元外部)
  • 如果您想双向无限滚动不起作用
相关问题