如何将背景图像添加到UICollectionView中,以滚动和缩放Will单元格 [英] How to add a background image to UICollectionView that will scroll and zoom will cells

查看:561
本文介绍了如何将背景图像添加到UICollectionView中,以滚动和缩放Will单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用UICollectionView构建镶嵌视图.

I'm building a mosaic view using UICollectionView.

我已经将UICollectionViewFlowLayout细分为一个固定的网格,该网格可以水平和垂直滚动.我还附加了UIPinchGestureRecognizer,以便可以缩放/缩放集合.

I have subclassed UICollectionViewFlowLayout to layout a fixed grid that can be scrolled both horizontally and vertically. I have also attached a UIPinchGestureRecognizer so the collection can scaled/zoomed.

集合中的每个单元格都包含一个具有一定透明度的UIImage.我想添加一个背景图像,该图像将随单元格滚动和缩放.

Each cell in the collection contains a UIImage with some transparency. I want to add a background image that will scroll and zoom with the cells.

我尝试了多种不同的解决方案.

I've attempted a number of different solutions.

  • 使用 colorWithPatternImage 设置UICollectionView的背景颜色. (不使用内容滚动/调整大小)
  • 在每个单元格上将背景图像视图设置为背景图像的相关裁剪部分. (使用太多内存)
  • setting the background color of the UICollectionView using colorWithPatternImage. (does not scroll/resize with content)
  • setting a background image view on each cell to the relevant cropped portion of the background image. (uses far too much memory)

我一直在研究补充视图和装饰视图,但一直在努力寻求解决之道.我认为我需要使用 Supplementary 视图,因为背景中使用的图像会随datasource改变.

I've been looking into Supplementary and Decoration Views but struggling to get my head around it. I think I need to use Supplementary views as the image used in the background will change depending on the datasource.

我不了解的是如何只注册一个补充视图来跨越整个collectionview的宽度和高度.它们似乎与indexPath即每个单元格相关.

What I don't understand is how I can register just one Supplementary View to span the width and height of the whole collectionview. They seem to be tied to an indexPath i.e each cell.

推荐答案

不知道您是否找到答案...!

Don't know if you found an answer...!

您在正确的道路上,希望使用补充视图.补充视图的索引路径不与单元格绑定,它具有自己的索引路径.

You're on the right track with wanting to use supplementary views. The index path of the supplementary view isn't tied to a cell, it has its own index path.

然后在UICollectionViewFlowLayout的子类中,需要子类化一些方法. 文档很好!

Then in your subclass of UICollectionViewFlowLayout you need to subclass a few methods. The docs are pretty good!

In the layoutAttributesForElementsInRect: method you'll need to call super and then add another set of layout attributes for your supplementary view.

然后在

在集合视图数据源中,您需要此方法:

In your collection view data source you need this method: 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;
    }
}

希望所有这些都可以使您走上正确的轨道:)

Hopefully all that should get you on the right track :)

这篇关于如何将背景图像添加到UICollectionView中,以滚动和缩放Will单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆