当前位置: 首页 > news >正文

水平方向瀑布流

水平方向瀑布流

 

效果

 

源码

https://github.com/YouXianMing/Animations

//
//  GridFlowLayoutViewController.m
//  Animations
//
//  Created by YouXianMing on 16/5/5.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import "GridFlowLayoutViewController.h"
#import "UIView+SetRect.h"
#import "GridLayout.h"
#import "FlowStyleCell.h"
#import "FileManager.h"
#import "NSString+MD5.h"
#import "NSData+JSONData.h"
#import "ResponseData.h"
#import "Math.h"
#import "GCD.h"

static NSString *picturesSource = @"http://www.duitang.com/album/1733789/masn/p/0/50/";

@interface GridFlowLayoutViewController () <UICollectionViewDataSource, UICollectionViewDelegate, GridLayoutDelegate>

@property (nonatomic, strong) UICollectionView  *collectionView;
@property (nonatomic)         CGFloat            rowHeight;
@property (nonatomic, strong) NSMutableArray    *datas;
@property (nonatomic, strong) ResponseData      *picturesData;
@property (nonatomic, strong) NSMutableArray    <WaterfallPictureModel *> *dataSource;

@end

@implementation GridFlowLayoutViewController

- (void)setup {
    
    [super setup];
    
    _dataSource = [NSMutableArray new];
    
    // 初始化布局文件
    CGFloat gap               = 1;
    NSInteger rowCount        = arc4random() % 3 + 2;
    _rowHeight                = (self.contentView.height - (rowCount + 1) * gap) / (CGFloat)rowCount;
    GridLayout *layout        = [GridLayout new];
    layout.manager.edgeInsets = UIEdgeInsetsMake(gap, gap, gap, gap);
    layout.manager.gap        = gap;
    layout.delegate           = self;
    
    NSMutableArray *rowHeights = [NSMutableArray array];
    for (int i = 0; i < rowCount; i++) {
        
        [rowHeights addObject:@(_rowHeight)];
    }
    layout.manager.rowHeights = rowHeights;
    
    self.collectionView                                = [[UICollectionView alloc] initWithFrame:self.contentView.bounds
                                                                            collectionViewLayout:layout];
    self.collectionView.delegate                       = self;
    self.collectionView.dataSource                     = self;
    self.collectionView.backgroundColor                = [UIColor clearColor];
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.alpha                          = 0;
    [self.collectionView registerClass:[FlowStyleCell class] forCellWithReuseIdentifier:@"FlowStyleCell"];
    [self.contentView addSubview:self.collectionView];
    
    // 获取数据
    [GCDQueue executeInGlobalQueue:^{
        
        NSString *string       = [picturesSource lowerMD532BitString];
        NSString *realFilePath = [FileManager theRealFilePath:[NSString stringWithFormat:@"~/Documents/%@", string]];
        NSData   *data         = nil;
        
        if ([FileManager fileExistWithRealFilePath:realFilePath] == NO) {
            
            data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:picturesSource]];
            [data writeToFile:realFilePath atomically:YES];
            
        } else {
            
            data = [NSData dataWithContentsOfFile:realFilePath];
        }
        
        NSDictionary *dataDic = [data toListProperty];
        
        [GCDQueue executeInMainQueue:^{
            
            self.picturesData = [[ResponseData alloc] initWithDictionary:dataDic];
            if (self.picturesData.success.integerValue == 1) {
                
                for (int i = 0; i < self.picturesData.data.blogs.count; i++) {
                    
                    [_dataSource addObject:self.picturesData.data.blogs[i]];
                }
                
                [_collectionView reloadData];
                [UIView animateWithDuration:0.5f animations:^{
                    
                    _collectionView.alpha = 1.f;
                }];
            }
        }];
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    WaterfallPictureModel *pictureModel = _dataSource[indexPath.row];
    
    FlowStyleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FlowStyleCell" forIndexPath:indexPath];
    cell.indexPath      = indexPath;
    cell.data           = pictureModel;
    cell.rowHeight      = _rowHeight;
    [cell loadContent];
    
    return cell;
}

- (CGFloat)itemWidthWithIndexPath:(NSIndexPath *)indexPath {
    
    WaterfallPictureModel *pictureModel = _dataSource[indexPath.row];
    
    return  [Math resetFromSize:CGSizeMake(pictureModel.iwd.floatValue, pictureModel.iht.floatValue)
                withFixedHeight:_rowHeight].width;
}

@end

 

细节

继承UICollectionViewLayout

重载UICollectionViewLayout的四个方法

部分实现细节

 

转载于:https://www.cnblogs.com/YouXianMing/p/5460903.html

相关文章:

  • log4j配置概要
  • [Assignment] C++1
  • linux之GDB常用命令汇总
  • uva1368DNA consensus string统计
  • static关键字的使用总结
  • 设计模式学习-原型模式
  • Centos 基础开发环境搭建之Maven私服nexus
  • HDU 2098 分拆素数和
  • 设计模式——9.装饰模式
  • unable to start the virtual device;Genymotion启动安卓模拟器出错
  • MapReduce编程job概念原理
  • 转载 asp.net的Request.ServerVariables参数说明
  • Extjs 学习总结-代理
  • consul笔记
  • Java设计模式图文详解
  • 【腾讯Bugly干货分享】从0到1打造直播 App
  • Angular Elements 及其运作原理
  • Hibernate【inverse和cascade属性】知识要点
  • httpie使用详解
  • Java 9 被无情抛弃,Java 8 直接升级到 Java 10!!
  • Java深入 - 深入理解Java集合
  • Leetcode 27 Remove Element
  • LintCode 31. partitionArray 数组划分
  • log4j2输出到kafka
  • Tornado学习笔记(1)
  • ⭐ Unity 开发bug —— 打包后shader失效或者bug (我这里用Shader做两张图片的合并发现了问题)
  • 爱情 北京女病人
  • 闭包--闭包之tab栏切换(四)
  • 湖南卫视:中国白领因网络偷菜成当代最寂寞的人?
  • 开发了一款写作软件(OSX,Windows),附带Electron开发指南
  • 类orAPI - 收藏集 - 掘金
  • 扑朔迷离的属性和特性【彻底弄清】
  • 如何选择开源的机器学习框架?
  • 删除表内多余的重复数据
  • 看到一个关于网页设计的文章分享过来!大家看看!
  • Salesforce和SAP Netweaver里数据库表的元数据设计
  • 选择阿里云数据库HBase版十大理由
  • ​2021半年盘点,不想你错过的重磅新书
  • ​LeetCode解法汇总1410. HTML 实体解析器
  • # 日期待t_最值得等的SUV奥迪Q9:空间比MPV还大,或搭4.0T,香
  • #我与Java虚拟机的故事#连载09:面试大厂逃不过的JVM
  • (14)目标检测_SSD训练代码基于pytorch搭建代码
  • (7)STL算法之交换赋值
  • (C语言)二分查找 超详细
  • (delphi11最新学习资料) Object Pascal 学习笔记---第5章第5节(delphi中的指针)
  • (附源码)springboot 房产中介系统 毕业设计 312341
  • (附源码)小程序 交通违法举报系统 毕业设计 242045
  • (官网安装) 基于CentOS 7安装MangoDB和MangoDB Shell
  • (十一)图像的罗伯特梯度锐化
  • (算法)Travel Information Center
  • (一)spring cloud微服务分布式云架构 - Spring Cloud简介
  • (转)Linux下编译安装log4cxx
  • (转)淘淘商城系列——使用Spring来管理Redis单机版和集群版
  • ***监测系统的构建(chkrootkit )
  • .NET C# 使用 SetWindowsHookEx 监听鼠标或键盘消息以及此方法的坑