Expand current selected item and catch users' eyes.
Unlike default UICollectionView
can only display items with same fixed size,
DaiExpandCollectionView
can not only display items in two different sizes simultaneously but also change selected items with smooth animation.
DaiExpandCollectionView is available through CocoaPods.
- Add
pod 'DaiExpandCollectionView'
to your Podfile - Run
pod install
- Run
open App.xcworkspace
- Then
#import <DaiExpandCollectionView/DaiExpandCollectionView.h>
Drag 4 source files under folder DaiExpandCollectionView\DaiExpandCollectionView\
to your project.
DaiExpandCollectionView.h
DaiExpandCollectionView.m
DaiExpandCollectionViewFlowLayout.h
DaiExpandCollectionViewFlowLayout.m
and then import the main header file:#import "DaiExpandCollectionView.h"
DaiExpandCollectionView *daiExpandCollectionView = [DaiExpandCollectionView initWithFrame:self.view.bounds];
[daiExpandCollectionView registerClass:[ImageCollectionViewCell class] forCellWithReuseIdentifier:@"ImageCollectionViewCell"];
daiExpandCollectionView.expandDelegate = self;
[self.view addSubview:daiExpandCollectionView];
Note: Init DaiExpandCollectionView
using [DaiExpandCollectionView initWithFrame:]
instead of [[UICollectionView alloc] initWithFrame:collectionViewLayout:]
which used by default UICollectionView
.
Next, register UICollectionViewCell
and then set up expandDelegate
.
There are two required methods in DaiExpandCollectionViewDelegate
- (NSInteger)numberOfItemsInCollectionView:(UICollectionView *)collectionView;
Return the number of items (views) in the collection view.
For example:
- (NSInteger)numberOfItemsInCollectionView:(UICollectionView *)collectionView {
return 20;
}
means there are 20 items (views) in the collection view.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
Return a UICollectionViewCell
to be displayed at the specified index in the collection view.
For example:
- (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;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndex:(NSInteger)index;
Return the index of current selected item. For example:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndex:(NSInteger)index {
NSLog(@"selected : %d", index);
}
If you want use more than 3 items in row. Change your code from
self.daiExpandCollectionView = [DaiExpandCollectionView initWithFrame:frame];
to
self.daiExpandCollectionView = [DaiExpandCollectionView initWithFrame:frame itemsInRow:4];
or you can dynamic change the value itemsInRow
in the run time
self.daiExpandCollectionView.itemsInRow = 5;
DaiExpandCollectionView
will handle the animation automatic.
- iOS 7.0+
- iOS 8.0+ Tested
- iPhone / iPad
- Vertical only