美文网首页
瀑布流布局

瀑布流布局

作者: 羊驼先生丶 | 来源:发表于2016-07-02 12:05 被阅读24次
//
//  ZSWaterLayout.h
//  02-瀑布流
//
//  Created by ZhuiYi on 14/12/27.
//  Copyright (c) 2014年 ZhuiYi. All rights reserved.
//

#import <UIKit/UIKit.h>
@class ZSWaterLayout;

@protocol ZSWaterLayoutDelegate <NSObject>
/**
 *  返回四周的间距(默认为10)
 *
 *  @param waterLayout 布局
 *
 *  @return 四周的间距
 */
- (UIEdgeInsets)insetsInWaterLayout:(ZSWaterLayout *)waterLayout;
/**
 *  Supplementary View的四周间距
 *
 *  @param elementKind Supplementary View 类型
 *  @param indexPath   IndexPath
 *
 *  @return 四周间距
 */
- (UIEdgeInsets)insetsOfSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath;
/**
 *  返回有多少列(默认为3)
 *
 *  @param waterLayout 布局
 *
 *  @return 有多少列
 */
- (NSInteger)columNumberInwaterLayout:(ZSWaterLayout *)waterLayout;
/**
 *  返回每列的间距(默认为10)
 *
 *  @param waterLayout 布局
 *
 *  @return 每列的间距
 */
- (CGFloat)columInsetInWaterLayout:(ZSWaterLayout *)waterLayout;
/**
 *  返回每行的间距(默认为10)
 *
 *  @param waterLayout 布局
 *
 *  @return 每行的间距
 */
- (CGFloat)rowInsetInWaterLayout:(ZSWaterLayout *)waterLayout;

- (CGFloat)supplementaryViewHeightOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath;
@required
/**
 *  返回每个cell的高度
 *
 *  @param waterLayout 布局
 *  @param indexPath   cell的位置
 *  @param width       cell的宽度
 *
 *  @return 根据宽度来算出cell的高度
 */
- (CGFloat)waterLayout:(ZSWaterLayout *)waterLayout heightForItemAtIndexPath:(NSIndexPath *)indexPath width:(CGFloat)width;
@end

@interface ZSWaterLayout : UICollectionViewLayout

@property (nonatomic, weak) id<ZSWaterLayoutDelegate> delegate;

@end
#import "CommunityArticleWaterLayout.h"



// 默认每行最多显示多少列
static NSInteger defaultMaxCol = 3;
// 默认每行的间距
static CGFloat  defaultRowInset = 10;
// 默认每列的间距
static CGFloat  defaultColInset = 10;
// 默认的四周的间距
static CGFloat  defaultInset = 10;

@interface ZSWaterLayout ()
// 最大Y值的数组,用于确定cell应该放在哪一列
@property (nonatomic, strong) NSMutableArray *maxYArray;
// 布局的数组
@property (nonatomic, strong) NSMutableArray *attributesArray;
@end

@implementation ZSWaterLayout
#pragma mark - 懒加载
- (NSMutableArray *)maxYArray
{
    if (!_maxYArray)
    {
        _maxYArray = [NSMutableArray array];
    }
    return _maxYArray;
}
- (NSArray *)attributesArray
{
    if (!_attributesArray) {
        _attributesArray = [[NSMutableArray alloc] init];
    }
    return _attributesArray;
}
#pragma mark - 有新的可见范围时就刷新
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
    return YES;
}
#pragma mark - 这个方法比下面的方法调用少,计算就放在这个方法中
- (void)prepareLayout
{
    [self.maxYArray removeAllObjects];
    [self.attributesArray removeAllObjects];
    for (NSInteger i = 0; i < [self getColumNumber]; i ++)
    {
        self.maxYArray[i] = @0.0;
    }
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    for (NSInteger i = 0; i < count; i ++)
    {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath];
        [self.attributesArray addObject:attrs];
    }
}
#pragma mark - 可见范围内的cell布局
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSMutableArray *retArray = [NSMutableArray array];
    
    for (UICollectionViewLayoutAttributes *att in _attributesArray) {
        if (CGRectIntersectsRect(att.frame, rect)) {
            [retArray addObject:att];
        }
    }
    
    if ([self.delegate respondsToSelector:@selector(supplementaryViewHeightOfKind:atIndexPath:)]) {
        CGFloat height = [self.delegate supplementaryViewHeightOfKind:UICollectionElementKindSectionHeader atIndexPath:[NSIndexPath indexPathForItem:1 inSection:0]];
        if (([self getInsets].top + height) >= rect.origin.y) {
            [retArray addObject:[self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:[NSIndexPath indexPathForItem:1 inSection:0]]];
        }
    }
    return retArray;
//    return self.attributesArray;
}

#pragma mark - 对应位置的cell的布局
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 四周的间距
    UIEdgeInsets inset = [self getInsets];
    // 每列的间距
    CGFloat colInset = [self getColumInset];
    // 每行的间距
    CGFloat rowInset = [self getRowInset];
    // 总列数
    NSInteger maxCol = [self getColumNumber];
    CGFloat supplementaryHeight = [self getSupplementaryViewHeightOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath];
    // collection的宽度
    CGFloat collectionWidth = self.collectionView.bounds.size.width;
    // 每个cell的宽高
    CGFloat cellWidth = (collectionWidth - inset.left - inset.right - (maxCol - 1) * colInset) / maxCol;
    CGFloat cellHeight = [self.delegate waterLayout:self heightForItemAtIndexPath:indexPath width:cellWidth];
    // 最小的Y值
    CGFloat minMaxY = [self.maxYArray[0] doubleValue];
    // 最小Y值的列号
    NSInteger minCol = 0;
    for (int i = 0; i < maxCol; i ++) {
        CGFloat maxY = [self.maxYArray[i] doubleValue];
        if (maxY < minMaxY) {
            minMaxY = maxY;
            minCol = i;
        }
    }
    CGFloat cellX = inset.left + (colInset + cellWidth) * minCol;
    UIEdgeInsets supplementaryInsets = [self getSupplementaryViewInsetsOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath];
    CGFloat cellY = minMaxY == 0 ? (minMaxY + inset.top + supplementaryInsets.top + supplementaryHeight):(minMaxY+rowInset);
    UICollectionViewLayoutAttributes *attribute = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    attribute.frame = CGRectMake(cellX, cellY, cellWidth, cellHeight);
    // 刷新Y值的高度
    self.maxYArray[minCol] = @(CGRectGetMaxY(attribute.frame));
    return attribute;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewLayoutAttributes *attribute = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:elementKind withIndexPath:indexPath];
    if ([elementKind isEqualToString:UICollectionElementKindSectionFooter]) {
        attribute.frame = CGRectMake(0, 0, 0, 0);
        return attribute;
    }
    
    if ([self getSupplementaryViewHeightOfKind:elementKind atIndexPath:indexPath] == 0) {
        attribute.frame = CGRectMake(0, 0, 0, 0);
    }
    else {
        UIEdgeInsets insets = [self getSupplementaryViewInsetsOfKind:elementKind atIndexPath:indexPath];
        attribute.frame = CGRectMake(insets.left, insets.top, self.collectionView.bounds.size.width - insets.left - insets.right, [self getSupplementaryViewHeightOfKind:elementKind atIndexPath:(NSIndexPath *)indexPath]);
    }
    return attribute;
}
#pragma mark - cellectionView的滚动范围
- (CGSize)collectionViewContentSize
{
    if (self.maxYArray.count)
    {
        // 最大的Y值
        CGFloat maxMaxY = [self.maxYArray[0] doubleValue];
        for (int i = 0; i < [self getColumNumber]; i ++)
        {
            CGFloat maxY = [self.maxYArray[i] doubleValue];
            if (maxY > maxMaxY)
            {
                maxMaxY = maxY;
            }
        }
        return CGSizeMake(self.collectionView.bounds.size.width, maxMaxY+[self getInsets].bottom);
    }
    return CGSizeMake(0, 0);
}
#pragma mark - 根据代理得到列数
- (NSInteger)getColumNumber
{
    if ([self.delegate respondsToSelector:@selector(columNumberInwaterLayout:)])
    {
        return [self.delegate columNumberInwaterLayout:self];
    }
    return defaultMaxCol;
}
#pragma mark - 根据代理得到四周的间距
- (UIEdgeInsets)getInsets
{
    if ([self.delegate respondsToSelector:@selector(insetsInWaterLayout:)])
    {
        return [self.delegate insetsInWaterLayout:self];
    }
    return UIEdgeInsetsMake(defaultInset, defaultInset, defaultInset, defaultInset);
}
- (UIEdgeInsets)getSupplementaryViewInsetsOfKind:(NSString*)elementKind atIndexPath:(NSIndexPath *)indexPath {
    if ([self.delegate respondsToSelector:@selector(insetsOfSupplementaryViewOfKind:atIndexPath:)]) {
        return [self.delegate insetsOfSupplementaryViewOfKind:elementKind atIndexPath:indexPath];
    }
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
#pragma mark - 根据代理得到每列之间的间距
- (CGFloat)getColumInset
{
    if ([self.delegate respondsToSelector:@selector(columInsetInWaterLayout:)])
    {
        return [self.delegate columInsetInWaterLayout:self];
    }
    return defaultColInset;
}
#pragma mark - 根据代理得到每行之间的间距
- (CGFloat)getRowInset
{
    if ([self.delegate respondsToSelector:@selector(rowInsetInWaterLayout:)])
    {
        return [self.delegate rowInsetInWaterLayout:self];
    }
    return defaultRowInset;
}

- (CGFloat) getSupplementaryViewHeightOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath {
    if ([self.delegate respondsToSelector:@selector(supplementaryViewHeightOfKind:atIndexPath:)]) {
        return [self.delegate supplementaryViewHeightOfKind:elementKind atIndexPath:indexPath];
    }
    return 0;
}

@end

相关文章

  • 瀑布流布局 的学习

    1- 实现瀑布流布局效果 瀑布流效果瀑布流代码木桶布局效果木桶布局代码瀑布流布局 2- 实现一个新闻瀑布流新闻...

  • 瀑布流、木桶布局

    瀑布流 瀑布流效果代码 木桶布局 木桶布局效果(加载有点慢)代码

  • 瀑布流照片墙布局

    title: 瀑布流照片墙布局 瀑布流概念 瀑布流布局是错落式的布局方式。它有一个特点,整个布局以列为单位,下一个...

  • CSS经典布局

    圣杯布局 瀑布流

  • 瀑布流

    瀑布流布局瀑布流jsonp新闻页

  • 瀑布流布局_木桶布局

    题目1: 实现一个瀑布流布局效果 瀑布流 题目2:实现木桶布局效果 木桶布局 题目3:**实现一个新闻瀑布流新闻网...

  • 瀑布流和懒加载结合

    实现一个瀑布流布局效果 瀑布流

  • 瀑布流

    什么是瀑布流: 欣赏 pinterest 样式分析 瀑布流,又被称作为瀑布流式布局,是一种比较流行的网站页面布局方...

  • 瀑布流

    1、什么是瀑布流 瀑布流,又称瀑布流式布局。是比较流行的一种网站页面布局,视觉表现为参差不齐的多栏布局,随着页面滚...

  • 瀑布流的三种实现

    先来欣赏三个瀑布流的网站 pinterest 淘宝爱逛街 蘑菇街 什么是瀑布流? 瀑布流,又称瀑布流式布局。 这种...

网友评论

      本文标题:瀑布流布局

      本文链接:https://www.haomeiwen.com/subject/behfjttx.html