DaiExpandCollectionView 0.0.5

DaiExpandCollectionView 0.0.5

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布时间最新发布2016年11月

DaidoujiChen 维护。



  • DaidoujiChen

展开当前选定的项目,吸引用户注意。

演示

image

概述

与默认的 UICollectionView 只能以相同的固定大小显示项目不同,DaiExpandCollectionView 不但可以同时显示两个不同大小的项目,还可以通过平滑动画切换选定的项目。

安装

手动安装

将文件夹 DaiExpandCollectionView\DaiExpandCollectionView\ 下的 4 个源文件拖放到您的项目中。

DaiExpandCollectionView.h
DaiExpandCollectionView.m
DaiExpandCollectionViewFlowLayout.h
DaiExpandCollectionViewFlowLayout.m

然后导入主头文件: #import "DaiExpandCollectionView.h"

如何使用

第 1 步 : 初始化

DaiExpandCollectionView *daiExpandCollectionView = [DaiExpandCollectionView initWithFrame:self.view.bounds];
[daiExpandCollectionView registerClass:[ImageCollectionViewCell class] forCellWithReuseIdentifier:@"ImageCollectionViewCell"];
daiExpandCollectionView.expandDelegate = self;
[self.view addSubview:daiExpandCollectionView];

注意: 使用 [DaiExpandCollectionView initWithFrame:] 初始化 DaiExpandCollectionView,而不是默认使用的 UICollectionView[[UICollectionView alloc] initWithFrame:collectionViewLayout:]

接下来,注册 UICollectionViewCell,然后设置 expandDelegate

第 2 步 : 必需的代理方法

DaiExpandCollectionViewDelegate 中有两个必需的方法

- (NSInteger)numberOfItemsInCollectionView:(UICollectionView *)collectionView;

返回集合视图中项目(视图)的数量。

例如

- (NSInteger)numberOfItemsInCollectionView:(UICollectionView *)collectionView {
    return 20;
}

意味着集合视图中有 20 个项目(视图)。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;

返回在集合视图指定索引处显示的 UICollectionViewCell

例如

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"ImageCollectionViewCell";
    ImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", indexPath.row + 1]];
    return cell;
}

第 3 步 : 可选代理方法

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndex:(NSInteger)index;

返回当前选中项的索引。例如

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndex:(NSInteger)index {
    NSLog(@"selected : %d", index);
}

自定义

如果您想在一行中使用超过 3 个项目。将您的代码从

self.daiExpandCollectionView = [DaiExpandCollectionView initWithFrame:frame];

改为

self.daiExpandCollectionView = [DaiExpandCollectionView initWithFrame:frame itemsInRow:4];

或者您可以在运行时动态更改 itemsInRow 的值

self.daiExpandCollectionView.itemsInRow = 5;

DaiExpandCollectionView 将自动处理动画。

支持

  • iOS 7.0+
  • iOS 8.0+ 已测试
  • iPhone / iPad
  • 仅垂直