如何将一个背景图像添加到将滚动和缩放的单元格的UICollectionView

我正在使用UICollectionView构build马赛克视图。

我已经分类UICollectionViewFlowLayout来布局一个固定的网格,可以水平和垂直滚动。 我还附加了一个UIPinchGestureRecognizer,以便集合可以缩放/缩放。

集合中的每个单元格都包含具有某种透明度的UIImage。 我想添加一个背景图像,将滚动和放大单元格。

我尝试了许多不同的解决scheme。

  • 使用colorWithPatternImage设置UICollectionView的背景颜色。 ( 不滚动/调整内容大小
  • 将每个单元格上的背景图像视图设置为背景图像的相关裁剪部分。 ( 使用太多内存

我一直在寻找补充和装饰的意见,但努力让我的头。 我想我需要使用补充视图,因为在后台使用的图像将根据数据源而改变。

我不明白的是如何注册一个补充视图,以跨越整个集合视图的宽度和高度。 他们似乎被绑定到indexPath,即每个单元格。

不知道你是否find答案…!

你想要使用补充的意见是正确的轨道。 补充视图的索引path不绑定到一个单元格,它有它自己的索引path。

然后在你的UICollectionViewFlowLayout的子类中,你需要子类化几个方法。 文档非常好!

layoutAttributesForElementsInRect:方法中,需要调用super,然后为补充视图添加另一组布局属性。

然后在layoutAttributesForSupplementaryViewOfKind:atIndexPath:方法中,将返回属性的大小设置为集合视图内容的大小,以便图像填充所有内容,而不仅仅是框架。 你也可能想要设置z顺序,以确保它在单元格后面。 查看UICollectionViewLayoutAttributes的文档

 @implementation CustomFlowLayout -(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { NSMutableArray *attributes = [[super layoutAttributesForElementsInRect:rect] mutableCopy]; // Use your own index path and kind here UICollectionViewLayoutAttributes *backgroundLayoutAttributes = [self layoutAttributesForSupplementaryViewOfKind:@"background" atIndexPath:[NSIndexPath indexPathWithItem:0 inSection:0]]; [attributes addObject:backgroundLayoutAttributes]; return [attributes copy]; } -(UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath if ([kind isEqualToString:@"background"]) { UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:kind withIndexPath:indexPath]; attrs.size = [self collectionViewContentSize]; attrs.zIndex = -10; return attrs; } else { return [super layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:indexPath]; } } @end 

在你的集合视图数据源中,你需要这个方法: collectionView:viewForSupplementaryElementOfKind:atIndexPath:

 -(void)viewDidLoad { [super viewDidLoad]; // Setup your collection view UICollectionView *collectionView = [UICollectionView initWithFrame:self.view.bounds collectionViewLayout:[CustomFlowLayout new]]; [collectionView registerClass:[BackgroundReusableView class] forSupplementaryViewOfKind:@"background" withReuseIdentifier:@"backgroundView"]; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { if ([kind isEqualToString:@"background"]) { BackgroundReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"backgroundView" forIndexPath:indexPath]; // Set extra info return view; } else { // Shouldn't be called return nil; } } 

希望所有这些都能让你走上正轨:)