UICollectionViewFlowLayout

UICollectionViewFlowLayout

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

最佳答案

IIRC,这正是在UICollectionView上的the WWDC 2012 sessions中演示的。

10-08 14:02