博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
瀑布流效果
阅读量:6420 次
发布时间:2019-06-23

本文共 5839 字,大约阅读时间需要 19 分钟。

瀑布流效果

 

效果

 

源码

////  WaterfallLayoutController.m//  Animations////  Created by YouXianMing on 16/1/3.//  Copyright © 2016年 YouXianMing. All rights reserved.//#import "WaterfallLayoutController.h"#import "NSData+JSONData.h"#import "GCD.h"#import "ResponseData.h"#import "CHTCollectionViewWaterfallLayout.h"#import "WaterfallCell.h"#import "WaterfallHeaderView.h"#import "WaterfallFooterView.h"static NSString *picturesSouce    = @"http://www.duitang.com/album/1733789/masn/p/0/50/";static NSString *cellIdentifier   = @"WaterfallCell";static NSString *headerIdentifier = @"WaterfallHeader";static NSString *footerIdentifier = @"WaterfallFooter";@interface WaterfallLayoutController () 
@property (nonatomic, strong) UICollectionView *collectionView;@property (nonatomic, strong) NSMutableArray
*dataSource;@property (nonatomic, strong) ResponseData *picturesData;@end@implementation WaterfallLayoutController- (void)setup { [super setup]; self.backgroundView.backgroundColor = [UIColor blackColor]; // 数据源 _dataSource = [NSMutableArray new]; // 初始化布局 CHTCollectionViewWaterfallLayout *layout = [[CHTCollectionViewWaterfallLayout alloc] init]; // 设置布局 layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5); layout.headerHeight = 64; // headerView高度 layout.footerHeight = 0; // footerView高度 layout.columnCount = 3; // 几列显示 layout.minimumColumnSpacing = 5; // cell之间的水平间距 layout.minimumInteritemSpacing = 5; // cell之间的垂直间距 // 初始化collectionView _collectionView = [[UICollectionView alloc] initWithFrame:self.contentView.bounds collectionViewLayout:layout]; _collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; _collectionView.dataSource = self; _collectionView.delegate = self; _collectionView.backgroundColor = [UIColor clearColor]; // 注册cell以及HeaderView,FooterView [_collectionView registerClass:[WaterfallCell class] forCellWithReuseIdentifier:cellIdentifier ]; [_collectionView registerClass:[WaterfallHeaderView class] forSupplementaryViewOfKind:CHTCollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier]; [_collectionView registerClass:[WaterfallFooterView class] forSupplementaryViewOfKind:CHTCollectionElementKindSectionFooter withReuseIdentifier:footerIdentifier]; // 添加到视图当中 [self.contentView addSubview:_collectionView]; // 获取数据 [GCDQueue executeInGlobalQueue:^{ NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:picturesSouce]]; NSDictionary *dataDic = [data toListProperty]; [GCDQueue executeInMainQueue:^{ self.picturesData = [[ResponseData alloc] initWithDictionary:dataDic]; if (self.picturesData.success.integerValue == 1) { NSMutableArray *indexPaths = [NSMutableArray array]; for (int i = 0; i < self.picturesData.data.blogs.count; i++) { [_dataSource addObject:self.picturesData.data.blogs[i]]; [indexPaths addObject:[NSIndexPath indexPathForItem:i inSection:0]]; } [_collectionView insertItemsAtIndexPaths:indexPaths]; } }]; }];}#pragma mark - UICollectionViewDelegate- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"点击了 %@", _dataSource[indexPath.row]);}#pragma mark - UICollectionViewDataSource- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { // 数据源 return [_dataSource count];}- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { // 1个区 return 1;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { // 注册collectionViewCell WaterfallCell *cell = (WaterfallCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; cell.data = _dataSource[indexPath.row]; [cell loadContent]; return cell;}- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { UICollectionReusableView *reusableView = nil; if ([kind isEqualToString:CHTCollectionElementKindSectionHeader]) { reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerIdentifier forIndexPath:indexPath]; } else if ([kind isEqualToString:CHTCollectionElementKindSectionFooter]) { reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:footerIdentifier forIndexPath:indexPath]; } return reusableView;}#pragma mark - CHTCollectionViewDelegateWaterfallLayout- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { WaterfallPictureModel *pictureModel = _dataSource[indexPath.row]; return CGSizeMake(pictureModel.iwd.floatValue, pictureModel.iht.floatValue);}@end

细节

 

转载地址:http://yxmra.baihongyu.com/

你可能感兴趣的文章
揪出MySQL磁盘消耗迅猛的真凶
查看>>
和“C”的再遇
查看>>
一键安装kubernetes 1.13.0 集群
查看>>
RabbitMq的集群搭建
查看>>
spring boot + mybatis 同时访问多数据源
查看>>
URL中汉字转码
查看>>
[转]go正则实例
查看>>
Selector中关于顺序的注意事项
查看>>
小黑小波比.清空<div>标签内容
查看>>
Java中的ExceptionInInitializerError异常及解决方法
查看>>
Spring 注入bean时的初始化和销毁操作
查看>>
java线程同步原理(lock,synchronized)
查看>>
MyEclipse中使用Hql编辑器找不到Hibernate.cfg.xml文件解决方法
查看>>
yRadio以及其它
查看>>
第四节 对象和类
查看>>
闪迪(SanDisk)U盘防伪查询(官方网站)
查看>>
Android onMeasure方法介绍
查看>>
无锁数据结构
查看>>
MySQL的变量查看和设置
查看>>
android onNewIntent
查看>>