2

我正在尝试创建一个行为类似于 iOS 跳板屏幕的集合视图。这意味着我想要一个包含我所有项目的集合视图,并在网格下方有一个页面控制器(就像跳板屏幕一样)。我还想实现相同的编辑模式,这意味着我的图标处于编辑模式,我可以将它们从一页拖到另一页,这样一页就不必满了。我尝试将单个集合视图设置为水平滚动,但它并没有按我的意愿行事。首先,图标是在一个下方添加的,而不是在旁边添加一个另一个。其次,我没有这种方式的页面点,用户不能在不同的页面上放置图标我也尝试设置一个页面控制器,并在每个页面中放置集合视图。在这种情况下,在编辑模式下,我无法在屏幕之间切换。我想那是因为我有不止一个集合视图,而且它们不相互交互。我想到了另一种方式,使用滚动视图,并尝试在该滚动视图中放置一个集合视图。但这在我看来是一个非常不优雅的解决方案。

有谁知道如何创建这样的场景?

编辑:我认为我的问题的答案隐藏在这个问题中。它描述了我想要的相同情况:一个集合视图,水平滚动,分页,图标的顺序不是垂直流动,而是水平流动。那里有一个部分好的答案。我说它部分好,因为答案描述了 UICollectionViewFlowLayout 的子类,但是在上传的示例项目中,子类是 UICollectionViewLayout,我认为重点是维护 UICollectionViewFlowLayout 具有的功能。这个问题是一年前提出的,所以我相信有人有答案,或者至少,我希望。

4

1 回答 1

3

我对我的问题有部分解决方案。我确实使用了(博格丹的)答案来回答上面的链接问题。这是代码:

布局.h:

#import <UIKit/UIKit.h>

@interface V9Layout : UICollectionViewLayout

@property (nonatomic, assign) NSInteger itemsInOneRow;
@property (nonatomic, assign) NSInteger itemsInOneCol;
@property (nonatomic, assign) CGSize itemSize;

@end

布局.m:

#import "V9Layout.h"

@interface V9Layout()

-(void)calculateLayoutProperties;
-(int)pagesInSection:(NSInteger)section;

@property (nonatomic, strong) NSMutableArray *frames;
@property (nonatomic, assign) CGFloat lineSpacing;
@property (nonatomic, assign) CGFloat interitemSpacing;
@property (nonatomic, assign) CGSize pageSize;    
@end

@implementation V9Layout

@synthesize itemsInOneRow = _itemsInOneRow, itemsInOneCol = _itemsInOneCol, itemSize = _itemSize;
@synthesize lineSpacing = _lineSpacing, interitemSpacing = _interitemSpacing;
@synthesize frames = _frames;

- (id)init
{
    self = [super init];
    if (self) {
        [self setup];
    }

    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self) {
        [self setup];
    }

    return self;
}

- (void)setup
{
    self.itemsInOneRow = 3;
    self.itemsInOneCol = 4;
    self.frames = [NSMutableArray array];
}

-(void)calculateLayoutProperties {
    self.pageSize = self.collectionView.bounds.size;

    // The width of all line spacings is equal to width of ONE item. The same for interitem spacing

    CGFloat itemWidth = self.pageSize.width / (self.itemsInOneRow + 1); // +1 because we all spaces make width of one additional item
    CGFloat itemHeight = self.pageSize.height / (self.itemsInOneCol + 1); // the same
    NSLog(@"calculateLayoutProperties item width: %f, item height: %f", itemWidth, itemHeight);

    self.itemSize = CGSizeMake(itemWidth, itemHeight);
    // this part is only to make a global parameter, because I'm using this data to create the size of the UICollectionViewCell. I think I need to use a delegate method for the UICollectionViewCell to ask the UICollectionViewLayout for the calculated size of cell.
    NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];

    if (standardUserDefaults) {
        [standardUserDefaults setObject:[NSNumber numberWithFloat:itemWidth] forKey:@"cellWidth"];
        [standardUserDefaults setObject:[NSNumber numberWithFloat:itemHeight] forKey:@"cellHeight"];
        [standardUserDefaults synchronize];
    }

    // spacing for x
    self.lineSpacing = (self.pageSize.width - (self.itemsInOneRow * self.itemSize.width)) / (self.itemsInOneRow + 1);
    // spacing for y
    self.interitemSpacing = (self.pageSize.height - (self.itemsInOneCol * self.itemSize.height)) / (self.itemsInOneCol + 1);
}

-(int)pagesInSection:(NSInteger)section {
    NSLog(@"v9layout: numberOfItemsInSection is %ld", (long)[self.collectionView numberOfItemsInSection:section]);
    return ([self.collectionView numberOfItemsInSection:section] - 1) / (self.itemsInOneRow * self.itemsInOneCol)  + 1;
}

-(CGSize)collectionViewContentSize {
    // contentSize.width is equal to pages * pageSize.width
    NSInteger sections = 1;
    if ([self.collectionView respondsToSelector:@selector(numberOfSections)]) {
        sections = [self.collectionView numberOfSections];
    }
    int pages = 0;
    for (int section = 0; section < sections; section++) {
        pages = pages + [self pagesInSection:section];
    }
    return CGSizeMake(pages * self.pageSize.width, self.pageSize.height);
}

-(void)prepareLayout {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self calculateLayoutProperties];
    });

    [self.frames removeAllObjects];

    NSInteger sections = 1;
    if ([self.collectionView respondsToSelector:@selector(numberOfSections)]) {
        sections = [self.collectionView numberOfSections];
    }
    int pagesOffset = 0; // Pages that are used by prevoius sections
    int itemsInPage = self.itemsInOneRow * self.itemsInOneCol;
    for (int section = 0; section < sections; section++) {
        NSMutableArray *framesInSection = [NSMutableArray array];
        int pagesInSection = [self pagesInSection:section];
        int itemsInSection = [self.collectionView numberOfItemsInSection:section];
        for (int page = 0; page < pagesInSection; page++) {
            int itemsToAddToArray = itemsInSection - framesInSection.count;
            int itemsInCurrentPage = itemsInPage;
            if (itemsToAddToArray < itemsInPage) { // If there are less cells than expected (typically last page of section), we go only through existing cells.
                itemsInCurrentPage = itemsToAddToArray;
            }
            for (int itemInPage = 0; itemInPage < itemsInCurrentPage; itemInPage++) {
                CGFloat originX = (pagesOffset + page) * self.pageSize.width + self.lineSpacing + (itemInPage % self.itemsInOneRow) * (self.itemSize.width + self.lineSpacing);
                CGFloat originY = self.interitemSpacing + (itemInPage / self.itemsInOneRow) * (self.itemSize.height + self.interitemSpacing);
                CGRect itemFrame = CGRectMake(originX, originY, self.itemSize.width, self.itemSize.height);
                [framesInSection addObject:NSStringFromCGRect(itemFrame)];
            }
        }
        [self.frames addObject:framesInSection];

        pagesOffset += pagesInSection;
    }
}

-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    NSMutableArray *attributes = [NSMutableArray array];

    NSInteger sections = 1;
    if ([self.collectionView respondsToSelector:@selector(numberOfSections)]) {
        sections = [self.collectionView numberOfSections];
    }

    int pagesOffset = 0;
    int itemsInPage = self.itemsInOneRow * self.itemsInOneCol;
    for (int section = 0; section < sections; section++) {
        int pagesInSection = [self pagesInSection:section];
        int itemsInSection = [self.collectionView numberOfItemsInSection:section];
        for (int page = 0; page < pagesInSection; page++) {
            CGRect pageFrame = CGRectMake((pagesOffset + page) * self.pageSize.width, 0, self.pageSize.width, self.pageSize.height);

            if (CGRectIntersectsRect(rect, pageFrame)) {
                int startItemIndex = page * itemsInPage;
                int itemsInCurrentPage = itemsInPage;
                if (itemsInSection - startItemIndex < itemsInPage) {
                    itemsInCurrentPage = itemsInSection - startItemIndex;
                }
                for (int itemInPage = 0; itemInPage < itemsInCurrentPage; itemInPage++) {
                    UICollectionViewLayoutAttributes *itemAttributes = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:startItemIndex + itemInPage inSection:section]];
                    if (CGRectIntersectsRect(itemAttributes.frame, rect)) {
                        [attributes addObject:itemAttributes];
                    }
                }
            }
        }

        pagesOffset += pagesInSection;
    }
    return attributes;
}

-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    attributes.frame = CGRectFromString(self.frames[indexPath.section][indexPath.item]);
    return attributes;
}

@end

这实现了水平分页,单元格的顺序是彼此相邻而不是彼此下方。我仍然想添加拖放功能,以便用户可以更改单元格的顺序。我对此提出了这个问题。希望我能找到答案并更新这个答案。

于 2014-06-06T22:49:54.417 回答