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开发仿电商类APP首页实例

    iOS开发仿电商类APP首页实例

    本篇文章主要介绍了iOS开发仿电商类APP首页实例,主要是利用ui布局,具有一定的参考价值,有需要的可以了解一下。
    2016-11-11
  • iOS利用CoreImage实现人脸识别详解

    iOS利用CoreImage实现人脸识别详解

    OS的人脸识别从iOS 5(2011)就有了,不过一直没怎么被关注过。人脸识别API允许开发者不仅可以检测人脸,也可以检测到面部的一些特殊属性,比如说微笑或眨眼。下面这篇文章主要给大家介绍了iOS利用CoreImage实现人脸识别的相关资料,需要的朋友可以参考下。
    2017-05-05
  • iOS NSCache和NSUrlCache缓存类实现示例详解

    iOS NSCache和NSUrlCache缓存类实现示例详解

    这篇文章主要为大家介绍了iOS NSCache和NSUrlCache缓存类实现示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • iOS开发之UITableView左滑删除等自定义功能

    iOS开发之UITableView左滑删除等自定义功能

    今天来给大家介绍下iOS开发中UITableView左滑实现微信中置顶,删除等功能。对大家开发iOS具有一定的参考借鉴价值,有需要的朋友们一起来看看吧。
    2016-09-09
  • IOS 下获取 rootviewcontroller 的版本不同的问题解决办法

    IOS 下获取 rootviewcontroller 的版本不同的问题解决办法

    这篇文章主要介绍了IOS 下获取 rootviewcontroller 的版本不同的问题解决办法的相关资料,希望通过本文能帮助到大家,让大家遇到这种问题可以解决,需要的朋友可以参考下
    2017-10-10
  • 使用Xcode为iOS应用项目创建PCH文件的方法及应用示例

    使用Xcode为iOS应用项目创建PCH文件的方法及应用示例

    这篇文章主要介绍了使用Xcode为iOS应用项目创建PCH文件的方法及应用示例,PCH文件可以保留应用的很多的基础设置信息以供复用,需要的朋友可以参考下
    2016-03-03
  • 详解iOS游戏开发中Cocos2D的坐标位置关系

    详解iOS游戏开发中Cocos2D的坐标位置关系

    这篇文章主要介绍了iOS游戏开发中Cocos2D的坐标位置关系,Cocos2D是专门用来开发iOS游戏的开源框架,文中示例代码采用Objective-C语言,需要的朋友可以参考下
    2016-02-02
  • iOS实现背景滑动效果

    iOS实现背景滑动效果

    这篇文章主要为大家详细介绍了iOS实现背景滑动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • ios实现文件对比的方法

    ios实现文件对比的方法

    这篇文章主要介绍了ios实现文件对比的方法,主要是用到了filemanager,有需要的小伙伴可以参考下。
    2015-05-05
  • IOS开发之由身份证号码提取性别的实现代码

    IOS开发之由身份证号码提取性别的实现代码

    这篇文章主要介绍了IOS开发之由身份证号码提取性别的实现代码的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-07-07

最新评论