iOS 玩转collection布局.

图上的效果如何实现呢?可以利用自定义flowlayout实现,下面上代码:

vc:

#import "ViewController.h"
#import "MyCell.h"
#import "MyFlowLayout.h"

@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(strong,nonatomic)NSMutableArray *viewArr;
@property(assign,nonatomic)CGPoint last;
@property(strong,nonatomic)UICollectionView *myCollectionView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
//    [self playView];
    [self configCollectionView];
}
#pragma mark - 自定义folwlayout
-(void)configCollectionView{
    MyFlowLayout *flowLayout = [[MyFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(50, 50);
    flowLayout.minimumLineSpacing = 0;
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 70) collectionViewLayout:flowLayout];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    [self.myCollectionView registerClass:[MyCell class] forCellWithReuseIdentifier:@"MyCell"];
    [self.view addSubview:self.myCollectionView];
    self.myCollectionView.backgroundColor = [UIColor greenColor];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 100;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
    
    return cell;
}

flowlayout:

@implementation MyFlowLayout
-(void)prepareLayout{
    [super prepareLayout];
    [self.attrsArr removeAllObjects];
    [self creatAttrsArr];
}
-(void)creatAttrsArr{
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    for (int i = 0; i<count; i++) {
        NSIndexPath *indexpath = [NSIndexPath indexPathForItem:i inSection:0];
        UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexpath];
        [self.attrsArr addObject:attrs];
    }
}
-(CGSize)collectionViewContentSize{
    return CGSizeMake(600, 70);
}
-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
    return self.attrsArr;
}
-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    //一群cell距离view前部30,cell的头部间距40,且cell的大小为(50,50),则
    attrs.center = CGPointMake(30+(30)*indexPath.row, 35);
    attrs.size = CGSizeMake(50, 50);
    attrs.zIndex = -indexPath.row;
    return attrs;
}
-(NSMutableArray *)attrsArr{
    if (!_attrsArr) {
        _attrsArr = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _attrsArr;
}



mycell:

#import "MyCell.h"

@implementation MyCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.cornerRadius = frame.size.width/2.0;
        self.layer.masksToBounds = YES;
        
        self.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
    }
    return self;
}
@end

效果:

除此之外还可以实现各种牛逼的效果,如圆环相册布局,cell遍布球体布局。。等等等等,就不一一解释了,自己是先看看吧.


更多问题,欢迎加群讨论:565191947

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值