gpt4 book ai didi

ios6 - 使用 UICollectionView 创建书架

转载 作者:行者123 更新时间:2023-12-03 08:30:44 28 4
gpt4 key购买 nike

我将在我的一个项目中创建一个书架。基本要求如下:

  • 类似于 iBooks 书架
  • 很好地支持两个方向
  • 支持各种iOS设备(不同分辨率)
  • 支持删除和插入项目
  • 支持通过长按手势重新排序项目
  • 拉下第一行时显示隐藏 Logo

  • UICollectionView 是我想到的第一个选项。它很容易支持网格单元。所以我搜索了一下,发现了一些非常有用的教程:
    Bryan Hansen's UICollectionView custom layout tutorial
    Mark Pospesel's How to Add a Decoration View to a UICollectionView
    LXReorderableCollectionViewFlowLayout
    结果如下:(请忽略颜色不一致问题,因为我选择的图形还不完美。)
    enter image description here
    我做了什么:
  • 通过创建继承自 LXReorderableCollectionViewFlowLayout 的类来创建自定义布局(用于重新排序)继承自 UICollectionFlowLayout
  • 为显示 Logo 添加了装饰 View
  • 添加了用于显示书架的装饰 View

  • 但是我遇到了几个问题:
    1.如果项目可以一屏显示,我根本无法滚动
    然后我添加了以下代码,以使内容大小更大
    - (CGSize)collectionViewContentSize
    {
    return CGSizeMake(self.collectionView.bounds.size.width, self.collectionView.bounds.size.height+100);
    }
    然后我现在可以滚动了。在这里,我拉下第一行:
    enter image description here
    您可以看到 Logo 的装饰 View 正在工作。
    2. 但是我拉到最后一行的时候遇到了第二组问题:
    enter image description here
    可以看到绿色框部分没有添加装饰 View 。
    3、书架装饰 View 的背景越来越暗。 (请引用 same problem here
    4. 重新排序时书架栏有时会移动
    enter image description here
    我在这里列出了一些重要的代码:
    - (void)prepareLayout
    {
    // call super so flow layout can do all the math for cells, headers, and footers
    [super prepareLayout];

    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    NSMutableDictionary *shelfLayoutInfo = [NSMutableDictionary dictionary];

    // decoration view - emblem
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
    UICollectionViewLayoutAttributes *emblemAttributes =
    [UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:[EmblemView kind]
    withIndexPath:indexPath];
    emblemAttributes.frame = [self frameForEmblem:YES];
    dictionary[[EmblemView kind]] = @{indexPath: emblemAttributes};

    // Calculate where shelves go in a vertical layout
    int sectionCount = [self.collectionView numberOfSections];

    CGFloat y = 0;
    CGFloat availableWidth = self.collectionViewContentSize.width - (self.sectionInset.left + self.sectionInset.right);
    int itemsAcross = floorf((availableWidth + self.minimumInteritemSpacing) / (self.itemSize.width + self.minimumInteritemSpacing));

    for (int section = 0; section < sectionCount; section++)
    {
    y += self.headerReferenceSize.height;
    //y += self.sectionInset.top;

    int itemCount = [self.collectionView numberOfItemsInSection:section];
    int rows = ceilf(itemCount/(float)itemsAcross)+1; // add 2 more empty row which doesn't have any data
    for (int row = 0; row < rows; row++)
    {
    indexPath = [NSIndexPath indexPathForItem:row inSection:section];
    shelfLayoutInfo[indexPath] = [NSValue valueWithCGRect:CGRectMake(0,y, self.collectionViewContentSize.width, self.itemSize.height + DECORATION_HEIGHT)];
    y += self.itemSize.height;

    if (row < rows - 1)
    y += self.minimumLineSpacing;
    }

    y += self.sectionInset.bottom;
    y += self.footerReferenceSize.height;
    }

    dictionary[[ShelfView kind]] = shelfLayoutInfo;

    self.shelfLayoutInfo = dictionary;
    }


    - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
    {
    NSArray *attributesArrayInRect = [super layoutAttributesForElementsInRect:rect];

    // cell layout info
    for (BookShelfLayoutAttributes *attribs in attributesArrayInRect)
    {

    attribs.zIndex = 1;
    CATransform3D t = CATransform3DIdentity;
    t = CATransform3DTranslate(t, 0, 0, 40);
    attribs.transform3D = CATransform3DRotate(t, 15 * M_PI / 180, 1, 0, 0);
    }

    // Add our decoration views (shelves)
    NSMutableDictionary* shelfDictionary = self.shelfLayoutInfo[[ShelfView kind]];
    NSMutableArray *newArray = [attributesArrayInRect mutableCopy];

    [shelfDictionary enumerateKeysAndObjectsUsingBlock:^(id key, NSValue* obj, BOOL *stop) {

    if (CGRectIntersectsRect([obj CGRectValue], rect))
    {
    UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:[ShelfView kind] withIndexPath:key];
    attributes.frame = [obj CGRectValue];

    NSLog(@"decorationView rect = %@",NSStringFromCGRect(attributes.frame));
    attributes.zIndex = 0;
    //attributes.alpha = 0.5; // screenshots
    [newArray addObject:attributes];
    }
    }];

    attributesArrayInRect = [NSArray arrayWithArray:newArray];

    NSMutableDictionary* emblemDictionary = self.shelfLayoutInfo[[EmblemView kind]];
    NSMutableArray *newArray2 = [attributesArrayInRect mutableCopy];
    [emblemDictionary enumerateKeysAndObjectsUsingBlock:^(NSIndexPath *indexPath, UICollectionViewLayoutAttributes *attributes, BOOL *innerStop) {
    if (CGRectIntersectsRect(rect, attributes.frame)) {
    [newArray2 addObject:attributes];
    }
    }];

    attributesArrayInRect = [NSArray arrayWithArray:newArray2];

    return attributesArrayInRect;
    }
    如果您有足够的耐心阅读这篇文章并提供任何建议或建议,我将不胜感激。如果我能解决所有问题,我会发布完整的源代码。先感谢您。

    最佳答案

    我建议检查你的最后一行并执行 [self.collectionView ScrollEnable:NO]第一行相同,设置背景颜色清除 collectionView 和 collectionViewCell 以及背景 View 上设置的书架图像。

    关于ios6 - 使用 UICollectionView 创建书架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15846742/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com