@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong) UICollectionView *collectionView
@property (nonatomic,strong) CSStickyHeaderFlowLayout *flowLayout
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad]
self.navigationController.navigationBarHidden = YES
[self.view addSubview:self.collectionView]
[self reloadLayout]
}
- (void)reloadLayout {
CSStickyHeaderFlowLayout *layout = (id)self.flowLayout
if ([layout isKindOfClass:[CSStickyHeaderFlowLayout class]]) {
layout.parallaxHeaderReferenceSize = CGSizeMake(self.view.frame.size.width, 200)
layout.itemSize = CGSizeMake(layout.itemSize.height, layout.itemSize.height)
}
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 5
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 10
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class]) forIndexPath:indexPath]
cell.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.1]
return cell
}
//设置headerView的宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
return CGSizeMake([UIScreen mainScreen].bounds.size.width, 100)
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
CSSectionHeader *sectionHeader = [collectionView dequeueReusableSupplementaryViewOfKind:kind
withReuseIdentifier:NSStringFromClass([CSSectionHeader class])
forIndexPath:indexPath]
return sectionHeader
} else if ([kind isEqualToString:CSStickyHeaderParallaxHeader]) {
UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind
withReuseIdentifier:NSStringFromClass([CSParallaxHeader class])
forIndexPath:indexPath]
return reusableView
}
return nil
}
///创建UICollectionView
- (UICollectionView *)collectionView {
if (!_collectionView) {
_collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:self.flowLayout]
_collectionView.showsHorizontalScrollIndicator = NO
_collectionView.delegate = self
_collectionView.dataSource = self
_collectionView.backgroundColor = nil
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class])]
[_collectionView registerClass:[CSSectionHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([CSSectionHeader class])]
[_collectionView registerClass:[CSParallaxHeader class] forSupplementaryViewOfKind:CSStickyHeaderParallaxHeader withReuseIdentifier:NSStringFromClass([CSParallaxHeader class])]
}
return _collectionView
}
- (CSStickyHeaderFlowLayout *)flowLayout {
if (!_flowLayout) {
_flowLayout = [[CSStickyHeaderFlowLayout alloc] init]
_flowLayout.minimumInteritemSpacing = 10
_flowLayout.minimumLineSpacing = 10
_flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 0, 10)
_flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical
_flowLayout.itemSize = CGSizeMake(150, 150)
}
return _flowLayout
}
@end
