1

UICollectionViewFlowLayout 很棒,但是,我希望它稍微调整一下,以便 scrollDirection 与布局方向不同。我想要的示例是跳板主屏幕或表情符号键盘,您可以在其中向左/向右滑动,但单元格从左到右、从上到下排列(而不是从上到下、从左到右,因为它们是UICollectionViewFlowLayout 与 scrollDirection 设置为水平)。任何人都知道我可以如何轻松地对 FlowLayout 进行子类化以进行此更改?我很失望他们在 UICollectionViewFlowLayout 对象上除了 scrollDirection 之外没有 layoutDirection :(

@rdelmar 我实现了与您的解决方案类似的东西,并且有效(不像我想要的那样优雅),但我注意到偶尔项目会从集合中消失,除非重新绘制,这就是我不接受答案的原因。这是我最终得到的结果:

@interface UICollectionViewPagedFlowLayout : UICollectionViewFlowLayout
@property int width;
@property int height;
@end
@implementation UICollectionViewPagedFlowLayout
#warning MINOR BUG: sometimes symbols disappear
- (CGSize)collectionViewContentSize {
    CGSize size = [super collectionViewContentSize];
    // make sure it's wide enough to cover all the objects
    size.width = self.collectionView.bounds.size.width * [self.collectionView numberOfSections];
    return size;
}
- (NSArray*) layoutAttributesForElementsInRect:(CGRect)rect {
    NSArray *array = [super layoutAttributesForElementsInRect:rect];
    CGRect visibleRect;
    visibleRect.origin = self.collectionView.contentOffset;
    visibleRect.size = self.collectionView.bounds.size;

    for (UICollectionViewLayoutAttributes* attributes in array) {
        if (CGRectIntersectsRect(attributes.frame, rect)) {
            // see which section this is in
            CGRect configurableRect = UIEdgeInsetsInsetRect(visibleRect, self.sectionInset);
            int horizontalIndex = attributes.indexPath.row % self.width;
            int verticalIndex = attributes.indexPath.row / self.width;
            double xspace = (configurableRect.size.width - self.width * self.itemSize.width) / self.width;
            double yspace = (configurableRect.size.height - self.height * self.itemSize.height) / self.height;
            attributes.center = CGPointMake(attributes.indexPath.section * visibleRect.size.width + self.sectionInset.left + (self.itemSize.width + xspace) * horizontalIndex + self.itemSize.width / 2, self.sectionInset.top + (self.itemSize.height + yspace) * verticalIndex + self.itemSize.height / 2);
        }
    }
    return array;
}
@end
4

3 回答 3

1

IIRC,这正是在 UICollectionView的 WWDC 2012 会议中展示的。

于 2012-12-28T23:16:22.203 回答
1

我不太清楚,我猜取决于你的定义。我做了一个项目,我的布局与您正在寻找的内容接近。这将水平排列行,从左到右,从上到下。我的数据源是一个数组数组,其中每个子数组为一行提供数据。

#import "SimpleHorizontalLayout.h" // subclass of UICollectionViewFlowLayout
#define kItemSize 60

@implementation SimpleHorizontalLayout 


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

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


-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect {
    NSInteger minRow =  (rect.origin.x > 0)?  rect.origin.x/(kItemSize +20) : 0; // need to check because bounce gives negative values for x.
    NSInteger maxRow = rect.size.width/(kItemSize +20) + 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;
}
于 2012-12-28T23:17:37.210 回答
0

在 Apple 添加一个简单的标志来更改流布局的布局方向之前,我最终还是进行了子类化。有些东西是特定于我的实现的,但它可能对其他人有帮助:

@interface UICollectionViewPagedFlowLayout : UICollectionViewFlowLayout
@property int width;
@property int height;
@end
@implementation UICollectionViewPagedFlowLayout
#warning MINOR BUG: sometimes items disappear
- (CGSize)collectionViewContentSize {
    CGSize size = [super collectionViewContentSize];
    size.width = self.collectionView.bounds.size.width * [self.collectionView numberOfSections];
    return size;
}
- (NSArray*) layoutAttributesForElementsInRect:(CGRect)rect {
    NSArray *array = [super layoutAttributesForElementsInRect:rect];
    CGRect visibleRect;
    visibleRect.origin = self.collectionView.contentOffset;
    visibleRect.size = self.collectionView.bounds.size;

    for (UICollectionViewLayoutAttributes* attributes in array) {
        if (CGRectIntersectsRect(attributes.frame, rect)) {
            // see which section this is in
            CGRect configurableRect = UIEdgeInsetsInsetRect(visibleRect, self.sectionInset);
            int horizontalIndex = attributes.indexPath.row % self.width;
            int verticalIndex = attributes.indexPath.row / self.width;
            double xspace = (configurableRect.size.width - self.width * self.itemSize.width) / self.width;
            double yspace = (configurableRect.size.height - self.height * self.itemSize.height) / self.height;
            attributes.center = CGPointMake(attributes.indexPath.section * visibleRect.size.width + self.sectionInset.left + (self.itemSize.width + xspace) * horizontalIndex + self.itemSize.width / 2, self.sectionInset.top + (self.itemSize.height + yspace) * verticalIndex + self.itemSize.height / 2);
        }
    }
    return array;
}
@end
于 2013-01-02T21:40:27.833 回答