SLCCollectionViewLayout 0.1.3

SLCCollectionViewLayout 0.1.3

yang666 维护。



  • 作者:
  • WeiKunChao

SLCCollectionViewLayout

Carthage compatible CocoaPods compatible License: MIT

一些常用的layout pod 'SLCCollectionViewLayout'

横向_横布局

系统默认横向_垂直布局。初始化如下

#import <SLCCollectionViewLayout/SLCCollectionHorizontalLayout.h>

- (UICollectionView *)collectionView {
if (!_collectionView) {

SLCCollectionHorizontalLayout *layout = [[SLCCollectionHorizontalLayout alloc] init];
layout.minimumLineSpacing = 5;
layout.minimumInteritemSpacing = 5;
layout.sectionInset = UIEdgeInsetsMake(0, 20, 0, 20);
layout.itemSize = CGSizeMake(44, 44);

_collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
_collectionView.backgroundColor = [UIColor whiteColor];
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.pagingEnabled = YES;
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"identify"];
}
return _collectionView;
}

Alt text.

缩放卡片

初始化: #import <SLCCollectionViewLayout/SLCCollectionViewAnimationCardLayout.h>

- (UICollectionView *)collectionView {
if (!_collectionView) {

SLCCollectionViewAnimationCardLayout *layout = [[SLCCollectionViewAnimationCardLayout alloc] init];
layout.minimumLineSpacing = 5;
layout.minimumInteritemSpacing = 5;
layout.sectionInset = UIEdgeInsetsMake(0, 20, 0, 20);
layout.itemSize = CGSizeMake(300, 500);
layout.widthScale = 0.7;
layout.heightScale = 0.8;

_collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
_collectionView.backgroundColor = [UIColor whiteColor];
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.pagingEnabled = YES;
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"identify"];
}
return _collectionView;
}

当翻页状态下,确保卡片居中需要设置三个修复方法

if (!_positionManager) {
_positionManager = [[SLCFixCardPagingPositionManager alloc] initWithCollectionView:self.collectionView];
}
return _positionManager;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[self.positionManager fixBegingDragging];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
[self.positionManager fixEndDragging];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[self.positionManager fixScroll];
}

指定到特定坐标

- (void)scrollToItemAtIndex:(NSInteger)index
animated:(BOOL)animated;

Alt text.

下拉header缩放

#import <SLCCollectionViewLayout/SLCCollectionViewHeaderZoomLayout.h> 初始化

- (UICollectionView *)collectionView {
if (!_collectionView) {

SLCCollectionViewHeaderZoomLayout *layout = [[SLCCollectionViewHeaderZoomLayout alloc] init];
layout.minimumLineSpacing = 5;
layout.minimumInteritemSpacing = 5;
layout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 200);
layout.headerReferenceSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 200);

_collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
_collectionView.backgroundColor = [UIColor whiteColor];
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.pagingEnabled = YES;
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"identify"];
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
}
return _collectionView;
}

Alt text.

流水布局

#import <SLCCollectionViewLayout/SLCCollectionViewWaterFallLayout.h> 初始化

- (UICollectionView *)collectionView {
if (!_collectionView) {

SLCCollectionViewWaterFallLayout *layout = [[SLCCollectionViewWaterFallLayout alloc] init];
layout.dataSource = self;
layout.minimumLineSpacing = 15;
layout.minimumInteritemSpacing = 20;
layout.sectionInset = UIEdgeInsetsMake(0, 20, 0, 20);
layout.lineCount = 2;

_collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
_collectionView.backgroundColor = [UIColor whiteColor];
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.pagingEnabled = YES;
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"identify"];
}
return _collectionView;
}

在 dataSource 内设置想要的 size.

- (CGFloat)SLCCollectionViewWaterFallLayout:(SLCCollectionViewWaterFallLayout *)layout heightAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row % 4 == 0) {
return 100;
}else if (indexPath.row % 4 == 1) {
return 150;
}else if (indexPath.row % 4 == 2) {
return 150;
}else {
return 100;
}
}

Alt text.