ViewController.m#
//
// ViewController.m
// CustomFlowLayout
//
#import "ViewController.h"
#import "TestCollectionViewCell.h"
#import "CustomFlowLayout.h"
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@end
@implementation ViewController
NSString *identifier = @"cell";
- (void)viewDidLoad {
[super viewDidLoad];
//創(chuàng)建布局對(duì)象
CustomFlowLayout *flowLayout = [[CustomFlowLayout alloc] init];
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 20, 414, 200) collectionViewLayout:flowLayout];
collectionView.delegate = self;
collectionView.dataSource = self;
[collectionView registerClass:[TestCollectionViewCell class] forCellWithReuseIdentifier:identifier];
[self.view addSubview:collectionView];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 20;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
TestCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.imgView.image = [UIImage imageNamed:@"10.png" ];
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
TestCollectionViewCell.h#
//
// TestCollectionViewCell.h
// CustomFlowLayout
//
//
#import <UIKit/UIKit.h>
@interface TestCollectionViewCell : UICollectionViewCell
@property(nonatomic,strong)UIImageView *imgView;
@end
TestCollectionViewCell.m#
//
// TestCollectionViewCell.m
// UI_21_CustomFlowLayout
//
//
#import "TestCollectionViewCell.h"
@implementation TestCollectionViewCell
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
self.imgView = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
[self.contentView addSubview:_imgView];
}
return self;
}
@end
CustomFlowLayout.m#
//
// CustomFlowLayout.m
// CustomFlowLayout
.
//
#import "CustomFlowLayout.h"
#define kItem 100
@implementation CustomFlowLayout
//做好一些準(zhǔn)備工作,初始化
//準(zhǔn)備布局
-(void)prepareLayout{
//設(shè)置大小
self.itemSize = CGSizeMake(kItem, kItem);
//設(shè)置滾動(dòng)方向
self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//最小行間距
self.minimumLineSpacing = 50;
}
//是否時(shí)刻改變并且重新布局
-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
return YES;
}
//數(shù)組里放置的是每一個(gè)item設(shè)置的屬性(例如: frame等等)
-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
//從父類里獲取item放置屬性的數(shù)組
NSArray *array = [super layoutAttributesForElementsInRect:rect];
//獲取屏幕中心點(diǎn)的x坐標(biāo)
CGFloat centerX = self.collectionView.frame.size.width/2 + self.collectionView.contentOffset.x;
//遍歷
for (UICollectionViewLayoutAttributes *attrs in array) {
//item的中心點(diǎn)
CGFloat itemCenterX = attrs.center.x;
//縮放比例
CGFloat scale = 1 + 0.5*(1 - ABS(itemCenterX - centerX))/200;
//
attrs.transform3D = CATransform3DMakeScale(scale, scale, 1);
}
return array;
}
@end
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者