XCCollectionViewLayout
UICollectionView瀑布流效果、仿射放大熟菲、滾動(dòng)放大效果看政,支持水平、垂直滾動(dòng)
使用pod導(dǎo)入文件或下載解壓后將XCollectionViewLayout文件夾導(dǎo)入項(xiàng)目中
pod 'XCollectionViewLayout'
導(dǎo)入頭文件
//瀑布流樣式 UICollectionViewLayout
#import "XCWaterCollectionViewLayout.h"
//滑動(dòng)放大抄罕、仿射放大樣式 UICollectionViewLayout
#import "XCollectionViewLayout.h"
滾動(dòng)方向 | 效果圖 |
---|---|
滾動(dòng)仿射放大效果 | |
滾動(dòng)放大效果 | |
垂直滾動(dòng) | |
水平滾動(dòng) |
導(dǎo)入XCollectionViewLayout.h
XCollectionViewLayout屬性及代理
typedef NS_ENUM(NSInteger,XCollectionViewLayoutTransformStyle){
XCollectionViewLayout_LinerTransform,//線性放大
XCollectionViewLayout_AffineTranform,//仿射旋轉(zhuǎn)放大
};
//滾動(dòng)放大模式(只支持橫向滾動(dòng))
@interface XCollectionViewLayout : UICollectionViewFlowLayout
@property (nonatomic) XCollectionViewLayoutTransformStyle transformStyle;
@property (nonatomic) CGFloat scaleCoefficient;//縮小系數(shù)允蚣,default 0.8,建議0.6~0.8
@end
滾動(dòng)仿射放大效果
XCollectionViewLayout *layout = [[XCollectionViewLayout alloc] init];
layout.transformStyle = XCollectionViewLayout_AffineTranform;
_collectView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
_collectView.backgroundColor = [UIColor whiteColor];
_collectView.delegate = self;
_collectView.dataSource = self;
[_collectView registerNib:[UINib nibWithNibName:@"LayoutControlCell" bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([LayoutControlCell class])];
[self.view addSubview:_collectView];
滾動(dòng)放大效果
XCollectionViewLayout *layout = [[XCollectionViewLayout alloc] init];
layout.transformStyle = XCollectionViewLayout_LinerTransform;
_collectView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
_collectView.backgroundColor = [UIColor whiteColor];
_collectView.delegate = self;
_collectView.dataSource = self;
[_collectView registerNib:[UINib nibWithNibName:@"LayoutControlCell" bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([LayoutControlCell class])];
[self.view addSubview:_collectView];
瀑布流效果
導(dǎo)入XCWaterCollectionViewLayout.h
XCTransformFlowLayout *layout = [[XCTransformFlowLayout alloc] init];
_collectView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
_collectView.backgroundColor = [UIColor lightGrayColor];
_collectView.delegate = self;
_collectView.dataSource = self;
[_collectView registerNib:[UINib nibWithNibName:@"LayoutControlCell" bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([LayoutControlCell class])];
[self.view addSubview:_collectView];
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(CGRectGetWidth(collectionView.frame) * 0.7, CGRectGetHeight(collectionView.frame) * 0.2);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(CGRectGetHeight(collectionView.frame) * 0.4, 0, CGRectGetHeight(collectionView.frame) * 0.4, 0);
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 20;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
LayoutControlCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([LayoutControlCell class]) forIndexPath:indexPath];
cell.numlabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.item];
return cell;
}
XCWaterCollectionViewLayout屬性及代理
@protocol XCWaterCollectionViewLayoutDelegate <NSObject>
/** 列數(shù) */
- (NSInteger)itemColumns;
/** item與item之間 水平間距 */
- (CGFloat)itemSpaceAtSectioin:(NSInteger)section;
/** item與item之間 垂直間距 */
- (CGFloat)itemLineSpaceAtSection:(NSInteger)section;
- (UIEdgeInsets)sectionInsetsAtSection:(NSInteger)section;
/** item 高度,XCWaterCollectionViewScrollDirectionVertical模式下返回item寬度 */
- (CGFloat)itemHeightAtIndexPath:(NSIndexPath *)indexPath;
/** header view */
- (CGSize)headerViewSizeAtSection:(NSInteger)section;
/** footer view */
- (CGSize)footerViewSizeAtSection:(NSInteger)section;
@end
@interface XCWaterCollectionViewLayout : UICollectionViewLayout
@property (nonatomic) NSInteger itemColumns;/**< default 2 */
@property (nonatomic) CGFloat itemSpace;/**< itemSpaceAtSectioin: */
@property (nonatomic) CGFloat lineSpace;/**< itemLineSpaceAtSection: */
@property (nonatomic) CGSize headerViewSize;/**< headerViewSizeAtSection: */
@property (nonatomic) CGSize footerSize;/**< footerViewSizeAtSection: */
@property (nonatomic) UIEdgeInsets sectioinInsets;/**< sectionInsetsAtSection: */
@property (nonatomic) CGFloat itemWidthOrHeight;/**< itemHeightAtIndexPath */
@property (nonatomic) XCWaterCollectionViewScrollDirection scrollDirection;//default XCWaterCollectionViewScrollDirectionVertical
@end