连续滚动视图;横向和纵向均可。

31

我已经在这个任务上挣扎了相当长的时间。我的目标是开发一个可以在垂直和水平方向上连续滚动的scrollview或collectionview。

以下是我认为应该看起来像的图片。透明的方框是从内存中重新加载的视图/单元格。只要一个视图/单元格移出屏幕,它就应该被重用于即将到来的新单元格..就像UITableViewController一样工作。

Continuous scroll

我知道UICollectionView只能做到水平或垂直无限滚动,而不能同时做到两者。但是,我不知道如何使用UIScrollView实现此目标。 我尝试了此问题答案附带的代码,我可以通过它重新创建视图(例如% 20),但那并不是我需要的..而且,它也不是连续的。

我知道这是可能的,因为HBO Go应用程序就是这样做的..我想要完全相同的功能。

我的问题:我如何实现我的目标?有没有指南/教程可以向我展示如何做?我找不到任何东西。


我有一个想法怎么做。我很快会发布一些代码。 - Petar
你真的需要它是无限的吗?我用一个重复20个单元格的集合视图完成了这个(类似于您的示例中的4行5项)。但是,如果我让它看起来在每个方向上都有超过大约1000行,滚动就会变得卡顿。但即使在每个方向上有1000个以上,它似乎也非常无限。 - rdelmar
@rdelmar 你所说的 "jerky" 是什么意思?是指它变慢了吗? - Paul Peelen
如果你在平移时,速度不会很慢,但如果你进行快速滑动,它会快速移动,然后暂停,然后恢复。对于1000个单元格或更少的情况下,我并没有真正注意到这一点。 - rdelmar
好的。我和我的PL讨论了你的建议,但是它没有被批准。我们有另一种方法要尝试...如果成功了,我会发布答案。 - Paul Peelen
显示剩余3条评论
4个回答

30

通过在距离中心一定距离后重新对UIScrollView进行居中操作,您可以获得无限滚动的效果。首先,您需要使contentSize足够大,以便可以滚动一点,因此我返回我的分区中项数的4倍和分区数的4倍,并在cellForItemAtIndexPath方法中使用mod运算符来获取正确的数组索引。然后,您必须在UICollectionView子类中重写layoutSubviews以进行重新居中(这在WWDC 2011视频“高级滚动视图技术”中有演示)。这是具有集合视图(在IB中设置)作为子视图的控制器类:

#import "ViewController.h"
#import "MultpleLineLayout.h"
#import "DataCell.h"

@interface ViewController ()
@property (weak,nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong,nonatomic) NSArray *theData;
@end

@implementation ViewController

- (void)viewDidLoad {
    self.theData = @[@[@"1",@"2",@"3",@"4",@"5"], @[@"6",@"7",@"8",@"9",@"10"],@[@"11",@"12",@"13",@"14",@"15"],@[@"16",@"17",@"18",@"19",@"20"]];
    MultpleLineLayout *layout = [[MultpleLineLayout alloc] init];
    self.collectionView.collectionViewLayout = layout;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.view.backgroundColor = [UIColor blackColor];
    [self.collectionView registerClass:[DataCell class] forCellWithReuseIdentifier:@"DataCell"];
    [self.collectionView reloadData];
}


- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
    return 20;
}

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
    return 16;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView  cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    DataCell *cell = [collectionView  dequeueReusableCellWithReuseIdentifier:@"DataCell" forIndexPath:indexPath];
    cell.label.text = self.theData[indexPath.section %4][indexPath.row %5];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
   // UICollectionViewCell *item = [collectionView cellForItemAtIndexPath:indexPath];
    NSLog(@"%@",indexPath);

}

这是UICollectionViewFlowLayout子类:

#define space 5
#import "MultpleLineLayout.h"

@implementation MultpleLineLayout { // a subclass of UICollectionViewFlowLayout
    NSInteger itemWidth;
    NSInteger itemHeight;
}

-(id)init {
    if (self = [super init]) {
        itemWidth = 60;
        itemHeight = 60;
    }
    return self;
}

-(CGSize)collectionViewContentSize {
    NSInteger xSize = [self.collectionView numberOfItemsInSection:0] * (itemWidth + space); // "space" is for spacing between cells.
    NSInteger ySize = [self.collectionView numberOfSections] * (itemHeight + space);
    return CGSizeMake(xSize, ySize);
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)path {
    UICollectionViewLayoutAttributes* attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:path];
    attributes.size = CGSizeMake(itemWidth,itemHeight);
    int xValue = itemWidth/2 + path.row * (itemWidth + space);
    int yValue = itemHeight + path.section * (itemHeight + space);
    attributes.center = CGPointMake(xValue, yValue);
    return attributes;
}


-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect {
    NSInteger minRow =  (rect.origin.x > 0)?  rect.origin.x/(itemWidth + space) : 0; // need to check because bounce gives negative values  for x.
    NSInteger maxRow = rect.size.width/(itemWidth + space) + minRow;
    NSMutableArray* attributes = [NSMutableArray array];
    for(NSInteger i=0 ; i < self.collectionView.numberOfSections; i++) {
        for (NSInteger j=minRow ; j < maxRow; j++) {
            NSIndexPath* indexPath = [NSIndexPath indexPathForItem:j inSection:i];
            [attributes addObject:[self layoutAttributesForItemAtIndexPath:indexPath]];
        }
    }
    return attributes;
}

最后,这是UICollectionView的子类:

-(void)layoutSubviews {
    [super layoutSubviews];
    CGPoint currentOffset = self.contentOffset;
    CGFloat contentWidth = self.contentSize.width;
    CGFloat contentHeight = self.contentSize.height;
    CGFloat centerOffsetX = (contentWidth - self.bounds.size.width)/ 2.0;
    CGFloat centerOffsetY = (contentHeight - self.bounds.size.height)/ 2.0;
    CGFloat distanceFromCenterX = fabsf(currentOffset.x - centerOffsetX);
    CGFloat distanceFromCenterY = fabsf(currentOffset.y - centerOffsetY);

    if (distanceFromCenterX > contentWidth/4.0) { // this number of 4.0 is arbitrary
        self.contentOffset = CGPointMake(centerOffsetX, currentOffset.y);
    }
    if (distanceFromCenterY > contentHeight/4.0) {
        self.contentOffset = CGPointMake(currentOffset.x, centerOffsetY);
    }
}

3
保罗,博客文章在哪里? - Moshe
我尝试实现您的代码。我得到了一个'gridLayout',但插入方法layoutSubviews会导致我的视图重复显示相同的内容。有时在滚动一点之后我也会接收到异常[UICollectionViewData validateLayoutInRect:]。你们中间有人有更多的建议吗,该如何让它正常工作? - Alex Cio
@rdelmar:我尝试了你的代码,但结果并不是我期望的那样。请查看这里的图片(http://www45.zippyshare.com/v/OF2tNh0T/file.html)。行和列重复出现多次,一行接着一行。这段代码曾经有效过吗?还是我做错了什么? - testing
@testing,是的,这段代码有效。我总是测试我在答案中发布的代码,所以你一定做错了些什么。 - rdelmar
@rdelmar:你用过storyboard吗?你在哪里子类化了UICollectionView?我在你的代码中没有看到它。你正在使用普通的UICollectionVIew类。DataCell是一个nib文件还是在代码中创建的?我认为是后者。collectionView是放在nib文件中并通过outlet连接的吗?当你通过Interface Builder添加一个collection view时,他需要一个重用标识符,并且在Interface Builder和代码中注册DataCell,这会导致黑屏。 - testing
显示剩余13条评论

9

@更新了Swift 3,并更改了maxRow的计算方式,否则最后一列将被截断并可能导致错误。

import UIKit

class NodeMap : UICollectionViewController {
    var rows = 10
    var cols = 10

    override func viewDidLoad(){
        self.collectionView!.collectionViewLayout = NodeLayout(itemWidth: 400.0, itemHeight: 300.0, space: 5.0)
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return rows
    }

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return cols
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        return collectionView.dequeueReusableCell(withReuseIdentifier: "node", for: indexPath)
    }
}

class NodeLayout : UICollectionViewFlowLayout {
    var itemWidth : CGFloat
    var itemHeight : CGFloat
    var space : CGFloat
    var columns: Int{
        return self.collectionView!.numberOfItems(inSection: 0)
    }
    var rows: Int{
        return self.collectionView!.numberOfSections
    }

    init(itemWidth: CGFloat, itemHeight: CGFloat, space: CGFloat) {
        self.itemWidth = itemWidth
        self.itemHeight = itemHeight
        self.space = space
        super.init()
    }

    required init(coder aDecoder: NSCoder) {
        self.itemWidth = 50
        self.itemHeight = 50
        self.space = 3
        super.init()
    }

    override var collectionViewContentSize: CGSize{
        let w : CGFloat = CGFloat(columns) * (itemWidth + space)
        let h : CGFloat = CGFloat(rows) * (itemHeight + space)
        return CGSize(width: w, height: h)
    }

    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
        let x : CGFloat = CGFloat(indexPath.row) * (itemWidth + space)
        let y : CGFloat = CGFloat(indexPath.section) + CGFloat(indexPath.section) * (itemHeight + space)
        attributes.frame = CGRect(x: x, y: y, width: itemWidth, height: itemHeight)
        return attributes
    }

    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        let minRow : Int = (rect.origin.x > 0) ? Int(floor(rect.origin.x/(itemWidth + space))) : 0
        let maxRow : Int = min(columns - 1, Int(ceil(rect.size.width / (itemWidth + space)) + CGFloat(minRow)))
        var attributes : Array<UICollectionViewLayoutAttributes> = [UICollectionViewLayoutAttributes]()
        for i in 0 ..< rows {
            for j in minRow ... maxRow {
                attributes.append(self.layoutAttributesForItem(at: IndexPath(item: j, section: i))!)
            }
        }
        return attributes
    }
}

1
@TreeBucket:它能够工作,但是在各个部分之间有空隙,我该如何移除那个空隙? - Abhishek Thapliyal
如果我必须在CollectionView的section中使用HeaderView,该怎么办?代码对此无效。我的意思是它没有显示HeaderView。 - Aditya Srivastava
@AbhishekThapliyal 你能够去掉那个空格吗? - user121095
我找到了间距的问题。我试图将它设置为0,但每一行之间仍然有一条细线。要消除此问题,请更改此行代码:let y: CGFloat = CGFloat(indexPath.section) * (itemHeight + space)。他将索引添加了两次。 - K. Janjuha

2
@rdelmar的答案非常好用,但我需要在Swift中进行。这是转换后的代码:)
class NodeMap : UICollectionViewController {
    @IBOutlet var activateNodeButton : UIBarButtonItem?
    var rows = 10
    var cols = 10
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return rows
    }
    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return cols
    }
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        return collectionView.dequeueReusableCellWithReuseIdentifier("node", forIndexPath: indexPath)
    }
    override func viewDidLoad() {
        self.collectionView!.collectionViewLayout = NodeLayout(itemWidth: 100.0, itemHeight: 100.0, space: 5.0)
    }
}

class NodeLayout : UICollectionViewFlowLayout {
    var itemWidth : CGFloat
    var itemHeight : CGFloat
    var space : CGFloat
    init(itemWidth: CGFloat, itemHeight: CGFloat, space: CGFloat) {
        self.itemWidth = itemWidth
        self.itemHeight = itemHeight
        self.space = space
        super.init()
    }
    required init(coder aDecoder: NSCoder) {
        self.itemWidth = 50
        self.itemHeight = 50
        self.space = 3
        super.init()
    }
    override func collectionViewContentSize() -> CGSize {
        let w : CGFloat = CGFloat(self.collectionView!.numberOfItemsInSection(0)) * (itemWidth + space)
        let h : CGFloat = CGFloat(self.collectionView!.numberOfSections()) * (itemHeight + space)
        return CGSizeMake(w, h)
    }
    override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes! {
        let attributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
        let x : CGFloat = CGFloat(indexPath.row) * (itemWidth + space)
        let y : CGFloat = CGFloat(indexPath.section) + CGFloat(indexPath.section) * (itemHeight + space)
        attributes.frame = CGRectMake(x, y, itemWidth, itemHeight)
        return attributes
    }
    override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? {
        let minRow : Int = (rect.origin.x > 0) ? Int(floor(rect.origin.x/(itemWidth + space))) : 0
        let maxRow : Int = Int(floor(rect.size.width/(itemWidth + space)) + CGFloat(minRow))
        var attributes : Array<UICollectionViewLayoutAttributes> = [UICollectionViewLayoutAttributes]()
        for i in 0...self.collectionView!.numberOfSections()-1 {
            for j in minRow...maxRow {
                attributes.append(self.layoutAttributesForItemAtIndexPath(NSIndexPath(forItem: j, inSection: i)))
            }
        }
        return attributes
    }
}

不错!我想我也会有用到这个的。 - Paul Peelen
在x与y、行与列、行与区段的概念上有点混淆,很难一直保持正确的方向感 :) 如果有人想复制粘贴这个,请给我留言,我会更新最新的更改。 - BadPirate
@BadPirate:程序能运行但是在各个部分之间有空格,要怎么去除掉呢? - Abhishek Thapliyal
如果我必须在CollectionView的section中使用HeaderView,该怎么办?代码对此无效。我的意思是它没有显示HeaderView。 - Aditya Srivastava

2
重置contentOffset可能是目前为止找到的最好解决方案。 无限滚动的最终结果 需要采取以下几个步骤:
  1. 在原始数据集的左右两侧填充额外的项目,以实现更大的可滚动区域; 这类似于具有大量重复数据集,但不同之处在于数量;
  2. 在开始时,计算集合视图的contentOffset以仅显示原始数据集(用黑色矩形绘制);
  3. 当用户向右滚动并且contentOffset达到触发值时,我们重置contentOffset以显示相同的视觉效果; 但实际上是不同的数据; 当用户向左滚动时,使用相同的逻辑。

enter image description here

因此,重要的工作是计算左右两侧应该填充多少项。如果您查看插图,您会发现至少需要在左侧填充一个额外的屏幕项目,并且在右侧另外填充一个额外的屏幕。填充的确切数量取决于原始数据集中有多少项以及您的项大小有多大。
我写了一篇关于这个解决方案的文章:

https://github.com/Alex1989Wang/Blogs/blob/master/contents/2018-03-24-Infinite-Scrolling-and-the-Tiling-Logic.md


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接