效果: 截图没有显示出抖动效果
//
// 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