ios UICollectionViewLayout 横向瀑布流

本文介绍了一个自定义UICollectionViewLayout的实现方法,通过该方法可以创建一个瀑布流布局效果的UICollectionView。文章详细解释了如何计算每个cell的位置和尺寸,并且提供了一个具体的UICollectionViewLayout子类——mierLayout的实现代码。

效果: 截图没有显示出抖动效果

 

//
//  mierLayout.m
//  diaojiba
//
//  Created by point on 16/9/4.
//  Copyright © 2016年 dacai. All rights reserved.
//

#import "mierLayout.h"

@interface mierLayout()

/** 存放所有cell的布局属性 */
@property (nonatomic, strong) NSMutableArray *attrsArray;





@property (nonatomic, assign) int row;

@property (nonatomic, assign) CGFloat currentX ;

@property (nonatomic, assign) CGFloat currentY ;

@property (nonatomic, assign) CGFloat preBtnW ;




@end


@implementation mierLayout

- (void)prepareLayout
{
    [super prepareLayout];
    _row = 0;
    _currentX = 0;
    _currentY = 0;
    _preBtnW = 0;
    
    // 清除之前所有的布局属性
    [self.attrsArray removeAllObjects];
    // 开始创建每一个cell对应的布局属性
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    for (NSInteger i = 0; i < count; i++) {
        // 创建位置
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        // 获取indexPath位置cell对应的布局属性
        UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath];
        [self.attrsArray addObject:attrs];
    }
    
}


- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    
    
    return self.attrsArray;
    
}


- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    CGFloat mainViewWidth = [UIScreen mainScreen].bounds.size.width;
    
    CGFloat w = 50+arc4random() % 100;
    CGFloat h = 30;
    CGFloat mw = 10;
    CGFloat mh = 10;
    CGFloat x = 0;
    CGFloat y = 0;
    
    
    static int  i;
    
    
    
    CGFloat btnWidth = w;
    
    if(i == 0 ){
        _currentX = 0;
        _preBtnW = btnWidth;
    }else {
        if(_preBtnW>mainViewWidth||(_currentX+mw+btnWidth+_preBtnW)>mainViewWidth){
            _row++;
            _currentX=0;
            _preBtnW = btnWidth;
        }else{
            _currentX += (mw+_preBtnW);
            _preBtnW = btnWidth;
        }
    }
    _currentY = _row*(h+mh);
    attrs.frame = CGRectMake(_currentX, _currentY, w, h);
    
    
    return attrs;
}







-(CATransform3D)getTransForm3DWithAngle:(CGFloat)angle{
    
    CATransform3D transform =CATransform3DIdentity;//获取一个标准默认的CATransform3D仿射变换矩阵
    transform=CATransform3DRotate(transform,angle,0,0,1);//获取旋转angle角度后的rotation矩阵。
    return transform;
    
}



- (CGSize)collectionViewContentSize{
    return CGSizeMake(0, 5000);
}


- (NSMutableArray *)attrsArray
{
    if (!_attrsArray) {
        _attrsArray = [NSMutableArray array];
    }
    return _attrsArray;
}

@end



 

转载于:https://my.oschina.net/zhaodacai/blog/743957

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值