ios - 连续滚动查看;水平和垂直

标签 ios uiscrollview uicollectionview infinite-scroll

我已经为这项任务苦苦挣扎了一段时间。我想开发的是一个 ScrollView 或 Collection View ,它可以在垂直和水平方向上连续滚动。

这是一张我认为应该是什么样子的图片。透明框是从内存中重新加载的 View /单元格。一旦 View /单元格离开屏幕,它就应该被重新用于即将到来的新单元格。就像 UITableViewController 的工作方式一样。

Continuous scroll

我知道 UICollectionView 只能水平或垂直无限滚动,不能同时无限滚动。但是,我不知道如何使用 UIScrollView 执行此操作。 我尝试了代码 attached to an answer on this question我可以让它重新创建 View (例如 % 20),但这并不是我真正需要的。此外,它不是连续的。

我知道这是可能的,因为 HBO Go 应用可以做到这一点。我想要完全相同的功能。

我的问题:我怎样才能实现我的目标?是否有任何指南/教程可以告诉我如何操作?我找不到。

最佳答案

您可以获得无限滚动,方法是在您离开中心一定距离后使用重新居中 UIScrollView 的技术。首先,您需要使 contentSize 足够大以便可以滚动一点,所以我返回 4 倍的项目数和 4 倍的部分数,并在中使用 mod 运算符cellForItemAtIndexPath 方法获取数组中的正确索引。然后,您必须重写 UICollectionView 的子类中的 layoutSubviews 才能重新居中(这在 WWDC 2011 视频“高级 ScrollView 技术”中进行了演示)。这是将 Collection View (在 IB 中设置)作为 subview 的 Controller 类:

#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);
    }
}

关于ios - 连续滚动查看;水平和垂直,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15549233/

相关文章:

xcode - xcode 6 zoomToRect动画问题

ios - 手动动画 UIScrollView.contentOffset 时背景图案跳跃

ios - 如何快速在collectionView中添加图像

ios - 按下后退按钮时音频停止播放

ios - ionic 3 响应状态为 : 0 for URL: null

ios - UIImageView setImage While UIScrollView is Scrolling

ios - 在加载之前为所有索引调用 sizeForItemAtIndexPath - 我很确定我遇到了 UICollectionView 错误或糟糕的设计

ios - 如何在方向更改时正确处理 UICollectionViewLayout 的 itemSize

ios - 在 RxSwift 中从 ViewModel 获取字符串到 View

ios - 从plist中选择一个随机字符串,并将其显示在文本字段中