ios让你简单实现瀑布流
2016-01-17 18:58
489 查看
以下是方法实现的代码
#import <UIKit/UIKit.h>
@interface XMFWaterFall : UICollectionViewLayout
// 一行的列数
@property (nonatomic, assign) NSUInteger colCount;
// collection内边距
@property (nonatomic, assign) UIEdgeInsets edgeInsets;
// cell的间距
@property (nonatomic, assign) CGFloat space;
// item宽
@property (nonatomic, assign) CGFloat itemWidth;
// 高度
@property (nonatomic, copy) CGFloat (^heightAction) (NSIndexPath *indexPath);
@end
#import "XMFWaterFall.h"
@interface XMFWaterFall ()
@property (nonatomic, strong) NSMutableArray<UICollectionViewLayoutAttributes *> *attributes;
@end
@implementation XMFWaterFall
// 第一步
- (void)prepareLayout {
[super prepareLayout];
NSUInteger totalCellNum = [self.collectionView numberOfItemsInSection:0];
_attributes = [NSMutableArray<UICollectionViewLayoutAttributes *> arrayWithCapacity: totalCellNum];
NSIndexPath *indexPath;
for (int i = 0; i < totalCellNum; i++) {
indexPath = [NSIndexPath indexPathForItem:i inSection:0];
[_attributes addObject: [self layoutAttributesForItemAtIndexPath:indexPath]];
}
}
// 第二步
- (CGSize)collectionViewContentSize {
CGFloat totalCellWidth = _edgeInsets.left + _edgeInsets.right + (_colCount - 1) * _space +_colCount * _itemWidth;
return CGSizeMake(totalCellWidth, [self getMaxY] + _edgeInsets.bottom);
}
// 第三步
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
NSUInteger currentColum = indexPath.row % _colCount;
CGFloat cellX = _edgeInsets.left + currentColum * (_itemWidth + _space); // cell的x
// cell的y
CGFloat cellY;
if (indexPath.row + 1 > _colCount) {
cellY = CGRectGetMaxY(_attributes[indexPath.row - _colCount].frame) + _space;
}
else
cellY = _edgeInsets.top;
// cell的高
CGFloat cellH = 0.0;
if (_heightAction) {
cellH = _heightAction(indexPath);
}
attributes.frame = CGRectMake(cellX, cellY, _itemWidth, cellH);
return attributes;
}
// 第三步
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
return _attributes;
}
/**
* 获得最大y值
*/
- (CGFloat)getMaxY {
NSUInteger totalCellNum = [self.collectionView numberOfItemsInSection:0];
NSInteger lastLenght = totalCellNum % _colCount;
if (lastLenght == 0) lastLenght = _colCount;
__block CGFloat tempY = 0.0;
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(_attributes.count - lastLenght, lastLenght)];
[_attributes enumerateObjectsAtIndexes:indexSet options:NSEnumerationConcurrent usingBlock:^(UICollectionViewLayoutAttributes * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
tempY = MAX(tempY, CGRectGetMaxY(obj.frame));
}];
return tempY;
}
@end
这是一种简单实现瀑布流的方法,注释不多,如有不懂可以在QQ群 389400205上问我
以下是我的github可以在此下载代码点击打开链接
#import <UIKit/UIKit.h>
@interface XMFWaterFall : UICollectionViewLayout
// 一行的列数
@property (nonatomic, assign) NSUInteger colCount;
// collection内边距
@property (nonatomic, assign) UIEdgeInsets edgeInsets;
// cell的间距
@property (nonatomic, assign) CGFloat space;
// item宽
@property (nonatomic, assign) CGFloat itemWidth;
// 高度
@property (nonatomic, copy) CGFloat (^heightAction) (NSIndexPath *indexPath);
@end
#import "XMFWaterFall.h"
@interface XMFWaterFall ()
@property (nonatomic, strong) NSMutableArray<UICollectionViewLayoutAttributes *> *attributes;
@end
@implementation XMFWaterFall
// 第一步
- (void)prepareLayout {
[super prepareLayout];
NSUInteger totalCellNum = [self.collectionView numberOfItemsInSection:0];
_attributes = [NSMutableArray<UICollectionViewLayoutAttributes *> arrayWithCapacity: totalCellNum];
NSIndexPath *indexPath;
for (int i = 0; i < totalCellNum; i++) {
indexPath = [NSIndexPath indexPathForItem:i inSection:0];
[_attributes addObject: [self layoutAttributesForItemAtIndexPath:indexPath]];
}
}
// 第二步
- (CGSize)collectionViewContentSize {
CGFloat totalCellWidth = _edgeInsets.left + _edgeInsets.right + (_colCount - 1) * _space +_colCount * _itemWidth;
return CGSizeMake(totalCellWidth, [self getMaxY] + _edgeInsets.bottom);
}
// 第三步
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
NSUInteger currentColum = indexPath.row % _colCount;
CGFloat cellX = _edgeInsets.left + currentColum * (_itemWidth + _space); // cell的x
// cell的y
CGFloat cellY;
if (indexPath.row + 1 > _colCount) {
cellY = CGRectGetMaxY(_attributes[indexPath.row - _colCount].frame) + _space;
}
else
cellY = _edgeInsets.top;
// cell的高
CGFloat cellH = 0.0;
if (_heightAction) {
cellH = _heightAction(indexPath);
}
attributes.frame = CGRectMake(cellX, cellY, _itemWidth, cellH);
return attributes;
}
// 第三步
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
return _attributes;
}
/**
* 获得最大y值
*/
- (CGFloat)getMaxY {
NSUInteger totalCellNum = [self.collectionView numberOfItemsInSection:0];
NSInteger lastLenght = totalCellNum % _colCount;
if (lastLenght == 0) lastLenght = _colCount;
__block CGFloat tempY = 0.0;
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(_attributes.count - lastLenght, lastLenght)];
[_attributes enumerateObjectsAtIndexes:indexSet options:NSEnumerationConcurrent usingBlock:^(UICollectionViewLayoutAttributes * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
tempY = MAX(tempY, CGRectGetMaxY(obj.frame));
}];
return tempY;
}
@end
这是一种简单实现瀑布流的方法,注释不多,如有不懂可以在QQ群 389400205上问我
以下是我的github可以在此下载代码点击打开链接
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 异步加载技术实现当滚动条到最底部的瀑布流效果
- 基于JavaScript实现瀑布流布局
- 原生JS实现美图瀑布流布局赏析
- avalonjs制作响应式瀑布流特效
- jquery 插件实现瀑布流图片展示实例
- jquery代码实现简单的随机图片瀑布流效果
- jQuery.lazyload+masonry改良图片瀑布流代码
- 讲解iOS开发中基本的定位功能实现
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- 浅析iOS应用开发中线程间的通信与线程安全问题
- 瀑布流布局并自动加载实现代码
- js实现的美女瀑布流效果代码