iOS实现水平方向瀑布流

 更新时间:2016年08月10日 11:58:03   作者:YouXianMing  
这篇文章主要为大家详细介绍了iOS实现水平方向瀑布流的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

效果

源码: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的四个方法

部分实现细节

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • iOS实现百度外卖头像波浪的效果

    iOS实现百度外卖头像波浪的效果

    对于现在很多人来说,叫外卖就成了不可或缺的习惯。某日瞬间发现百度外卖的APP波浪效果很是吸引人,相比较其他的外卖APP,颜值略高些.(淘宝也有波浪的效果),遂就思考如何实现这种"浪"的效果,下面来一起看看。
    2016-08-08
  • iOS开发之如何给View添加指定位置的边框线详解

    iOS开发之如何给View添加指定位置的边框线详解

    这篇文章主要给大家介绍了iOS开发之如何给View添加指定位置的边框线的相关资料,给view加边框很容易,重点是如何给指定边框加边框,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-10-10
  • iOS获取某个日期后n个月的日期

    iOS获取某个日期后n个月的日期

    这篇文章主要介绍了iOS获取某个日期后n个月的日期的相关资料,需要的朋友可以参考下
    2017-08-08
  • ios app重提提交审核流程

    ios app重提提交审核流程

    本篇文章给大家讲述了在APP第一次没有审核通过后,重新提交的流程和注意的地方,学习一下吧。
    2017-12-12
  • iOS实现文件切片储存并且上传(仿断点续传机制)

    iOS实现文件切片储存并且上传(仿断点续传机制)

    这篇文章主要给大家介绍了关于iOS实现文件切片储存并上传仿断点续传机制的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-12-12
  • iOS时钟开发案例分享

    iOS时钟开发案例分享

    这篇文章主要为大家分享了iOS时钟开发案例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-04-04
  • iOS中的UITableView的重用机制与加载优化详解

    iOS中的UITableView的重用机制与加载优化详解

    本篇文章主要介绍了iOS中的UITableView的重用机制与加载优化详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02
  • iOS渐变圆环旋转动画CAShapeLayer CAGradientLayer

    iOS渐变圆环旋转动画CAShapeLayer CAGradientLayer

    这篇文章主要介绍了iOS渐变圆环旋转动画CAShapeLayer CAGradientLayer的相关资料,需要的朋友可以参考下
    2016-09-09
  • iOS实现微信/QQ显示最近拍摄图片的功能实例代码

    iOS实现微信/QQ显示最近拍摄图片的功能实例代码

    如果你刚刚拍摄了图片,在使用微信/QQ发生消息时会显示“你可能要发送的图片”,这个功能非常人性化,怎么实现的呢?下面小编给大家分享iOS实现微信/QQ显示最近拍摄图片的功能实例代码,一起看看吧
    2017-03-03
  • iOS GCD之dispatch_group_enter和dispatch_group_leave使用

    iOS GCD之dispatch_group_enter和dispatch_group_leave使用

    这篇文章主要为大家介绍了iOS GCD之dispatch_group_enter和dispatch_group_leave使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03

最新评论