用CHTCollectionViewWaterfallLayout写瀑布流

用CHTCollectionViewWaterfallLayout写瀑布流

用CHTCollectionViewWaterfallLayout写瀑布流

用CHTCollectionViewWaterfallLayout写瀑布流

实现的瀑布流效果图:

用CHTCollectionViewWaterfallLayout写瀑布流用CHTCollectionViewWaterfallLayout写瀑布流

用CHTCollectionViewWaterfallLayout写瀑布流用CHTCollectionViewWaterfallLayout写瀑布流

源码:

WaterfallCell.h 与 WaterfallCell.m

//
//  WaterfallCell.h
//  UICollectionView
//
//  Created by YouXianMing on 14-9-17.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface WaterfallCell : UICollectionViewCell

@property (nonatomic, strong) UIImageView *showImageView;

@end
//
//  WaterfallCell.m
//  UICollectionView
//
//  Created by YouXianMing on 14-9-17.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "WaterfallCell.h"

@implementation WaterfallCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Scale the imageview to fit inside the contentView with the image centered:
        CGRect imageViewFrame = CGRectMake(0.f, 0.f,
                                           CGRectGetMaxX(self.contentView.bounds),
                                           CGRectGetMaxY(self.contentView.bounds));
        _showImageView                  = [UIImageView new];
        _showImageView.contentMode      = UIViewContentModeScaleAspectFill;
        _showImageView.frame            = imageViewFrame;
        _showImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _showImageView.clipsToBounds    = YES;
        [self addSubview:_showImageView];
        self.layer.borderWidth = 1.f;
    }
    return self;
}

@end


HeaderView.h 与 HeaderView.m

//
//  HeaderView.h
//  UICollectionView
//
//  Created by YouXianMing on 14-9-17.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface HeaderView : UICollectionReusableView

@end
//
//  HeaderView.m
//  UICollectionView
//
//  Created by YouXianMing on 14-9-17.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "HeaderView.h"

@implementation HeaderView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.borderWidth = 1.f;
    }
    return self;
}

@end


FooterView.h 与 FooterView.m

//
//  FooterView.h
//  UICollectionView
//
//  Created by YouXianMing on 14-9-17.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface FooterView : UICollectionReusableView

@end
//
//  FooterView.m
//  UICollectionView
//
//  Created by YouXianMing on 14-9-17.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "FooterView.h"

@implementation FooterView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.borderWidth = 1.f;
    }
    return self;
}

@end


使用时候的代码:

//
//  RootViewController.m
//  UICollectionView
//
//  Created by YouXianMing on 14-9-17.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "RootViewController.h"
#import "CHTCollectionViewWaterfallLayout.h"

#import "WaterfallCell.h"
#import "HeaderView.h"
#import "FooterView.h"

#define CELL_IDENTIFIER   @"WaterfallCell"
#define HEADER_IDENTIFIER @"WaterfallHeader"
#define FOOTER_IDENTIFIER @"WaterfallFooter"

@interface RootViewController ()<UICollectionViewDataSource, CHTCollectionViewDelegateWaterfallLayout>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray   *dataSource;
@property (nonatomic, strong) NSMutableArray   *dataSourceSize;

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 数据源
    _dataSource = [NSMutableArray new];
    for (int i = 0; i <= 16; i++)
    {
        [_dataSource addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d", i]]];
    }

    // 初始化布局
    CHTCollectionViewWaterfallLayout *layout = [[CHTCollectionViewWaterfallLayout alloc] init];
    
    // 设置布局
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.headerHeight = 100;           // headerView高度
    layout.footerHeight = 100;           // footerView高度
    layout.columnCount  = 4;             // 几列显示
    layout.minimumColumnSpacing    = 5;  // cell之间的水平间距
    layout.minimumInteritemSpacing = 5;  // cell之间的垂直间距
    
    // 初始化collectionView
    _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds
                                         collectionViewLayout:layout];
    _collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    _collectionView.dataSource       = self;
    _collectionView.delegate         = self;
    _collectionView.backgroundColor  = [UIColor whiteColor];
    
    // 注册cell以及HeaderView,FooterView
    [_collectionView registerClass:[WaterfallCell class]
        forCellWithReuseIdentifier:CELL_IDENTIFIER];
    [_collectionView registerClass:[HeaderView class]
        forSupplementaryViewOfKind:CHTCollectionElementKindSectionHeader
               withReuseIdentifier:HEADER_IDENTIFIER];
    [_collectionView registerClass:[FooterView class]
        forSupplementaryViewOfKind:CHTCollectionElementKindSectionFooter
               withReuseIdentifier:FOOTER_IDENTIFIER];
    
    // 添加到视图当中
    [self.view addSubview:_collectionView];
}

#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:CELL_IDENTIFIER
                                                                   forIndexPath:indexPath];
    
    UIImage *image           = _dataSource[indexPath.row];
    cell.showImageView.image = image;
    
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableView = nil;
    
    if ([kind isEqualToString:CHTCollectionElementKindSectionHeader])
    {
        reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                          withReuseIdentifier:HEADER_IDENTIFIER
                                                                 forIndexPath:indexPath];
    } else if ([kind isEqualToString:CHTCollectionElementKindSectionFooter])
    {
        reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                          withReuseIdentifier:FOOTER_IDENTIFIER
                                                                 forIndexPath:indexPath];
    }
    
    return reusableView;
}

#pragma mark - CHTCollectionViewDelegateWaterfallLayout
- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 用以返回尺寸
    UIImage *image = _dataSource[indexPath.row];
    return image.size;
}

@end

这个代码再怎么简单也会很复杂-_-!!

以下是使用的细节需要注意的地方:

设置的对应关系-

用CHTCollectionViewWaterfallLayout写瀑布流

cell中的图片可不是随便设置的-

用CHTCollectionViewWaterfallLayout写瀑布流

要注意返回每个cell的size值-

用CHTCollectionViewWaterfallLayout写瀑布流

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/109066.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • js闭包面试题经典_js闭包原理

    js闭包面试题经典_js闭包原理说明最近看到这样一段代码functionfun(n,o){console.log(o);return{fun:function(m){returnfun(m,n);}};}vara=fun(0);a.fun(1);a.fun(2);a.fun(3);varb=f

    2022年8月30日
    5
  • Java物联网方向_物联网专业课程安排——未来主要方向之一

    Java物联网方向_物联网专业课程安排——未来主要方向之一课程1、物联网产业与技术导论使用电子工业出版社《物联网:技术、应用、标准、安全与商业模式》等等教材。在学完高等数学,物理,化学,通信原理,数字电路,计算机原理,程序设计原理等课程后开设本课程,全面了解物联网之RFID、M2M、传感网、两化融合等技术与应用。课程2、C语言程序设计使用清华大学出版社《C语言程序设计》等教材。物联网涉及底层编程,C语言为必修课,同时需要了解OSGi,OPC,S…

    2022年7月7日
    28
  • Android Intent.FLAG_ACTIVITY_NEW_TASK的个人理解「建议收藏」

    Android Intent.FLAG_ACTIVITY_NEW_TASK的个人理解「建议收藏」首先分四部曲简单做一下说明1.What(是什么):Intent类中的一个静态标志属性publicstaticfinalintFLAG_ACTIVITY_NEW_TASK=0x10000000;2.Why(为什么要使用):在特殊情况下,如果不加这个标志,会报错(下文详细说明)3.When(什么时候使用):当调用startActivity启动一个Activity时4.How(如何

    2022年10月6日
    2
  • dede织梦后台页面及功能修改及精简操作方法

    dede织梦后台页面及功能修改及精简操作方法

    2021年9月19日
    37
  • 虚拟 显示器_win7检测不到第二个显示器

    虚拟 显示器_win7检测不到第二个显示器这里写自定义目录标题虚拟显示器概述虚拟显示器原理方案虚拟显示器概述最近因工作需要,需要在物理显卡上虚拟出一个显示器,我是用的操作系统是win7,查询了下官方文档和网络资源,没有现成的框架,所以需要自己研究如何一步步实现。虚拟显示器用途挺多的,我们其实在一些场景中有用到过,比如displaylink,我们网上买的一些USB转HDMI的连接线,在使用过程中我们需要安装displaylink的驱…

    2022年8月21日
    9
  • 关于Anycast的介绍[通俗易懂]

    关于Anycast的介绍[通俗易懂]Anycast最初是在RFC1546中提出并定义的,它的最初语义是,在IP网络上通过一个Anycast地址标识一组提供特定服务的主机,同时服务访问方并不关心提供服务的具体是哪一台主机(比如DNS或者镜像服务),访问该地址的报文可以被IP网络路由到这一组目标中的任何一台主机上,它提供的是一种无状态的、尽力而为的服务(如图1)。目前对于Anycast的中文译称主要有,“任播”、“泛潘”、“选播”等,本

    2022年5月24日
    142

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号