-
瀑布流(OC)
-
算封装的比较好。提供接口,只需要成为MGWaterFlayout的代理,即可在外界设置那个cell的间距,collection View 的inset等进行UI界面的布局,非常方便。
-
github网址:
https://github.com/LYM-mg/MGPuBuLiuDemo
MGWaterflowLayout
//
// MGWaterflowLayout.m
// MGPuBuLiuDemo
//
// Created by ming on 16/6/9.
// Copyright © 2016年 ming. All rights reserved.
//
#import "MGWaterflowLayout.h"
/** 默认的列数 */
static const NSInteger MGDefaultColumnCount = 3;
/** 每一列之间的间距 */
static const CGFloat MGDefaultColumnMargin = 10;
/** 每一行之间的间距 */
static const CGFloat MGDefaultRowMargin = 10;
/** 边缘间距 */
static const UIEdgeInsets MGDefaultEdgeInsets = {10, 10, 10, 10};
@interface MGWaterflowLayout ()
/** 存放所有cell的布局属性 */
@property (nonatomic, strong) NSMutableArray *attrsArray;
/** 存放所有列的当前高度 */
@property (nonatomic, strong) NSMutableArray *columnHeights;
/** 每一行之间的间距 */
- (CGFloat)rowMargin;
/** 每一列之间的间距 */
- (CGFloat)columnMargin;
/** 列数 */
- (NSInteger)columnCount;
/** 边缘间距 */
- (UIEdgeInsets)edgeInsets;
@end
@implementation MGWaterflowLayout
#pragma mark - 常见数据处理
- (CGFloat)rowMargin
{
if ([self.delegate respondsToSelector:@selector(rowMarginInWaterflowLayout:)]) {
return [self.delegate rowMarginInWaterflowLayout:self];
} else {
return MGDefaultRowMargin;
}
}
- (CGFloat)columnMargin
{
if ([self.delegate respondsToSelector:@selector(columnMarginInWaterflowLayout:)]) {
return [self.delegate columnMarginInWaterflowLayout:self];
} else {
return MGDefaultColumnMargin;
}
}
- (NSInteger)columnCount
{
if ([self.delegate respondsToSelector:@selector(columnCountInWaterflowLayout:)]) {
return [self.delegate columnCountInWaterflowLayout:self];
} else {
return MGDefaultColumnCount;
}
}
- (UIEdgeInsets)edgeInsets
{
if ([self.delegate respondsToSelector:@selector(edgeInsetsInWaterflowLayout:)]) {
return [self.delegate edgeInsetsInWaterflowLayout:self];
} else {
return MGDefaultEdgeInsets;
}
}
#pragma mark - 懒加载
- (NSMutableArray *)columnHeights
{
if (!_columnHeights) {
_columnHeights = [NSMutableArray array];
}
return _columnHeights;
}
- (NSMutableArray *)attrsArray
{
if (!_attrsArray) {
_attrsArray = [NSMutableArray array];
}
return _attrsArray;
}
- (void)prepareLayout{
[super prepareLayout];
#warning 删除以前保存的高度
[self.columnHeights removeAllObjects];
for (NSInteger i = 0; i < self.columnCount; i++) {
[self.columnHeights addObject:@(self.edgeInsets.top)];
}
// 清除之前所有的布局属性
[self.attrsArray removeAllObjects];
// 1.获取cell的个数
NSInteger count = [self.collectionView numberOfItemsInSection:0];
// 清除之前所有的布局属性
[self.attrsArray removeAllObjects];
// 2.遍历cell,把每一个布局添加到数组
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];
}
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
// 创建布局属性
UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
CGFloat x,y,w,h;
// w = (self.collectionView.frame.size.width - [self edgeInsets].left - [self edgeInsets].right - ([self columnCount] - 1) * MGDefaultColumnMargin)/[self columnCount];
w = (self.collectionView.frame.size.width - self.edgeInsets.left - self.edgeInsets.right - (self.columnCount - 1) * self.columnMargin) / self.columnCount;
// 通过代理可以设置高度
if ([self.delegate respondsToSelector:@selector(waterflowLayout:heightForItemAtIndex:itemWidth:)]) {
h = [self.delegate waterflowLayout:self heightForItemAtIndex:indexPath itemWidth:w];
}else{
h = 70 + arc4random_uniform(100);
}
// 取得所有列中高度最短的列
NSInteger minHeightColumn = [self minHeightColumn];
// x = MGDefaultEdgeInsets.left + minHeightColumn * (w + MGDefaultColumnMargin);
x = self.edgeInsets.left + minHeightColumn * (w + self.columnMargin);
y = MGDefaultEdgeInsets.top + [self.columnHeights[minHeightColumn] floatValue];
#warning 更改最短的一列
[self.columnHeights replaceObjectAtIndex:minHeightColumn withObject:@(y+h)];
attrs.frame = CGRectMake(x, y, w, h);
return attrs;
}
/**
* 决定cell的排布
* 第一次显示时会调用一次
* 用力拖拽时也会调一次
*/
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
return self.attrsArray;
}
/// 返回collectionView的滚动范围
- (CGSize)collectionViewContentSize{
if (self.columnHeights.count == 0) {
return CGSizeMake(0, 0);
}
// 获得最高的列
NSInteger maxColum = [self maxHeightColumn];
CGFloat height = [self.columnHeights[maxColum] floatValue] + MGDefaultEdgeInsets.bottom;
CGFloat width = self.collectionView.frame.size.width;
return CGSizeMake(width, height);
}
/// 取得所有列中高度最短的列
- (NSInteger)minHeightColumn{
// 找出columnHeights的最小值
NSInteger minHeightColum = 0;
CGFloat minColumHeight = [self.columnHeights[0] floatValue];
for (NSInteger i = 1; i < self.columnHeights.count; i++) {
CGFloat tempHeight = [self.columnHeights[i] floatValue];
if (tempHeight < minColumHeight) {
minHeightColum = i;
minColumHeight = tempHeight;
}
}
return minHeightColum;
}
/// 取得所有列中高度最高的列
- (NSInteger)maxHeightColumn{
// 找出columnHeights的最小值
NSInteger maxHeightColumn = 0;
CGFloat maxColumnHeight = [self.columnHeights[0] floatValue];
for (NSInteger i = 1; i < self.columnHeights.count; i++) {
CGFloat tempHeight = [self.columnHeights[i] floatValue];
if (tempHeight > maxColumnHeight) {
maxHeightColumn = i;
maxColumnHeight = tempHeight;
}
}
return maxHeightColumn;
}
@end
ViewController
//
// ViewController.m
// MGPuBuLiuDemo
//
// Created by ming on 16/6/9.
// Copyright © 2016年 ming. All rights reserved.
//
#import "ViewController.h"
#import "MGWaterflowLayout.h"
#import "MGShopCell.h"
#import "MGShopModel.h"
#import "MJRefresh.h"
#import "MJExtension.h"
static NSString *const ShopCellIdentifier = @"ShopCellIdentifier";
@interface ViewController ()<UICollectionViewDataSource,MGWaterflowLayoutDelegate>
/** 所有的商品数据 */
@property (nonatomic, strong) NSMutableArray *shops;
@property (nonatomic, weak) UICollectionView *collectionView;
@end
@implementation ViewController
- (NSMutableArray *)shops
{
if (!_shops) {
_shops = [NSMutableArray array];
}
return _shops;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setUpLayout];
[self setupRefresh];
// 第一次刷新手动调用
[self.collectionView.header beginRefreshing];
}
/// 初始化collectionView
- (void)setUpLayout{
// 创建布局
MGWaterflowLayout *flowLayout = [[MGWaterflowLayout alloc] init];
flowLayout.delegate = self;
// 创建CollectionView
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
collectionView.backgroundColor = [UIColor whiteColor];
collectionView.dataSource = self;
[self.view addSubview:collectionView];
// 注册
[collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([MGShopCell class]) bundle:nil] forCellWithReuseIdentifier:ShopCellIdentifier];
[self.view addSubview:collectionView];
self.collectionView = collectionView;
}
/// 刷新加载数据
- (void)setupRefresh{
// 上拉刷新
self.collectionView.header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSArray *shops = [MGShopModel objectArrayWithFilename:@"1.plist"];
[self.shops removeAllObjects];
[self.shops addObjectsFromArray:shops];
// 刷新数据
[self.collectionView reloadData];
[self.collectionView.header endRefreshing];
});
}];
// 下拉刷新
self.collectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSArray *shops = [MGShopModel objectArrayWithFilename:@"1.plist"];
[self.shops addObjectsFromArray:shops];
// 刷新数据
[self.collectionView reloadData];
[self.collectionView.footer endRefreshing];
});
}];
// 隐藏下拉刷新
self.collectionView.footer.hidden = YES;
}
#pragma mark - <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
// return 39;
self.collectionView.footer.hidden = self.shops.count == 0;
return self.shops.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
MGShopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ShopCellIdentifier forIndexPath:indexPath];
// cell.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1.0];
cell.shop = self.shops[indexPath.item];
return cell;
}
#pragma mark - <MGWaterflowLayoutDelegate>
- (CGFloat)waterflowLayout:(MGWaterflowLayout *)waterflowLayout heightForItemAtIndex:(NSIndexPath *)indexPath itemWidth:(CGFloat)itemWidth
{
MGShopModel *shop = self.shops[indexPath.item];
return itemWidth * shop.h / shop.w;
}
- (CGFloat)rowMarginInWaterflowLayout:(MGWaterflowLayout *)waterflowLayout
{
return 20;
}
- (CGFloat)columnCountInWaterflowLayout:(MGWaterflowLayout *)waterflowLayout
{
if (self.shops.count <= 50) return 2;
return 3;
}
- (UIEdgeInsets)edgeInsetsInWaterflowLayout:(MGWaterflowLayout *)waterflowLayout
{
return UIEdgeInsetsMake(10, 20, 30, 10);
}
@end
-
github
| 项目 | 简介 |
| : | : |
| MGDS_Swif | 逗视视频直播 |
| MGMiaoBo | 喵播视频直播 |
| MGDYZB | 斗鱼视频直播 |
| MGDemo | n多小功能合集 |
| MGBaisi | 高度仿写百思 |
| MGSinaWeibo | 高度仿写Sina |
| MGLoveFreshBeen | 一款电商App |
| MGWeChat | 小部分实现微信功能 |
| MGTrasitionPractice | 自定义转场练习 |
| DBFMDemo | 豆瓣电台 |
| MGPlayer | 一个播放视频的Demo |
| MGCollectionView | 环形图片排布以及花瓣形排布 |
| MGPuBuLiuDemo | 瀑布流--商品展 |
| MGSlideViewDemo | 一个简单点的侧滑效果,仿QQ侧滑 |
| MyResume | 一个展示自己个人简历的Demo |
| GoodBookDemo | 好书 |
-
2、逗视:逗你玩的直播App,可下载试玩
看下效果