设为首页 加入收藏

TOP

水平方向瀑布流(一)
2017-10-13 10:29:19 】 浏览:8671
Tags:水平 方向 瀑布

水平方向瀑布流

 

效果

 

源码

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];
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇iOS - UITableView中Cell重用机制.. 下一篇IOS 杂笔-19(属性与变量的优缺..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目