From 7b02207537d35bfa1714bf8beafc921f717d100a Mon Sep 17 00:00:00 2001 From: 单军华 Date: Wed, 11 Jul 2018 10:47:42 +0800 Subject: [PATCH] 首次上传 --- screendisplay/screendisplay/Classes/Base/BaseView/LMJVerticalFlowLayout.m | 216 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 216 insertions(+), 0 deletions(-) diff --git a/screendisplay/screendisplay/Classes/Base/BaseView/LMJVerticalFlowLayout.m b/screendisplay/screendisplay/Classes/Base/BaseView/LMJVerticalFlowLayout.m new file mode 100755 index 0000000..52dc0a6 --- /dev/null +++ b/screendisplay/screendisplay/Classes/Base/BaseView/LMJVerticalFlowLayout.m @@ -0,0 +1,216 @@ +// +// LMJVerticalFlowLayout.m +// ��������������������� +// +// Created by apple on 16/7/31. +// Copyright �� 2016��� NJHu. All rights reserved. +// + +#import "LMJVerticalFlowLayout.h" +#define LMJXX(x) floorf(x) +#define LMJXS(s) ceilf(s) + +static const NSInteger LMJ_Columns_ = 3; +static const CGFloat LMJ_XMargin_ = 10; +static const CGFloat LMJ_YMargin_ = 10; +static const UIEdgeInsets LMJ_EdgeInsets_ = {20, 10, 10, 10}; + +@interface LMJVerticalFlowLayout() + +/** ���������cell���attrbts */ +@property (nonatomic, strong) NSMutableArray<UICollectionViewLayoutAttributes *> *lmj_AtrbsArray; + +/** ��������������������������� */ +@property (nonatomic, strong) NSMutableArray<NSNumber *> *lmj_ColumnsHeightArray; + +- (NSInteger)columns; + +- (CGFloat)xMargin; + +- (CGFloat)yMarginAtIndexPath:(NSIndexPath *)indexPath; + +- (UIEdgeInsets)edgeInsets; + +@end + +@implementation LMJVerticalFlowLayout + +/** + * ������������������������������������ + */ +- (void)prepareLayout +{ + [super prepareLayout]; + + //������������������������������������������������������ + [self.lmj_ColumnsHeightArray removeAllObjects]; + + //���������������������������, ������������������ + for (NSInteger i = 0; i < self.columns; i++) { + [self.lmj_ColumnsHeightArray addObject:@(self.edgeInsets.top)]; + } + + // ���������������������cells���attrbs + [self.lmj_AtrbsArray removeAllObjects]; + + // ������������������, ������cell���������atrbs, ��������������� + for (NSInteger i = 0; i < [self.collectionView numberOfItemsInSection:0]; i++) + { + [self.lmj_AtrbsArray addObject:[self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]]]; + } + +} + + +/** + *������������������������cell������������������������ + */ +- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath +{ + UICollectionViewLayoutAttributes *atrbs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; + + CGFloat w = 1.0 * (self.collectionView.frame.size.width - self.edgeInsets.left - self.edgeInsets.right - self.xMargin * (self.columns - 1)) / self.columns; + + w = LMJXX(w); + + // ���������������������, ������������������������������ + CGFloat h = [self.delegate waterflowLayout:self collectionView:self.collectionView heightForItemAtIndexPath:indexPath itemWidth:w]; + + // ���������������������������������������, ���������0��������� + __block NSInteger indexCol = 0; + __block CGFloat minColH = [self.lmj_ColumnsHeightArray[indexCol] doubleValue]; + + [self.lmj_ColumnsHeightArray enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { + CGFloat colH = obj.floatValue; + if (minColH > colH) { + minColH = colH; + indexCol = idx; + } + }]; + + CGFloat x = self.edgeInsets.left + (self.xMargin + w) * indexCol; + + CGFloat y = minColH + [self yMarginAtIndexPath:indexPath]; + + // ������������ + if (minColH == self.edgeInsets.top) { + y = self.edgeInsets.top; + } + + // ������frame + atrbs.frame = CGRectMake(x, y, w, h); + + // ���������������������������;��������������� + self.lmj_ColumnsHeightArray[indexCol] = @(CGRectGetMaxY(atrbs.frame)); + + return atrbs; +} + +// layoutAttributesForElementsInRect +- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect +{ + return self.lmj_AtrbsArray; +} + + +- (CGSize)collectionViewContentSize +{ + CGFloat maxColH = [self.lmj_ColumnsHeightArray.firstObject doubleValue]; + + for (NSInteger i = 1; i < self.lmj_ColumnsHeightArray.count; i++) + { + CGFloat colH = [self.lmj_ColumnsHeightArray[i] doubleValue]; + if(maxColH < colH) + { + maxColH = colH; + } + } + + return CGSizeMake(self.collectionView.frame.size.width, maxColH + self.edgeInsets.bottom); +} + + +- (NSMutableArray *)lmj_AtrbsArray +{ + if(_lmj_AtrbsArray == nil) + { + _lmj_AtrbsArray = [NSMutableArray array]; + } + return _lmj_AtrbsArray; +} + +- (NSMutableArray *)lmj_ColumnsHeightArray +{ + if(_lmj_ColumnsHeightArray == nil) + { + _lmj_ColumnsHeightArray = [NSMutableArray array]; + } + return _lmj_ColumnsHeightArray; +} + +- (NSInteger)columns +{ + if([self.delegate respondsToSelector:@selector(waterflowLayout:columnsInCollectionView:)]) + { + return [self.delegate waterflowLayout:self columnsInCollectionView:self.collectionView]; + } + else + { + return LMJ_Columns_; + } +} + +- (CGFloat)xMargin +{ + if([self.delegate respondsToSelector:@selector(waterflowLayout:columnsMarginInCollectionView:)]) + { + return [self.delegate waterflowLayout:self columnsMarginInCollectionView:self.collectionView]; + } + else + { + return LMJ_XMargin_; + } +} + +- (CGFloat)yMarginAtIndexPath:(NSIndexPath *)indexPath +{ + if([self.delegate respondsToSelector:@selector(waterflowLayout:collectionView:linesMarginForItemAtIndexPath:)]) + { + return [self.delegate waterflowLayout:self collectionView:self.collectionView linesMarginForItemAtIndexPath:indexPath]; + }else + { + return LMJ_YMargin_; + } +} + +- (UIEdgeInsets)edgeInsets +{ + if([self.delegate respondsToSelector:@selector(waterflowLayout:edgeInsetsInCollectionView:)]) + { + return [self.delegate waterflowLayout:self edgeInsetsInCollectionView:self.collectionView]; + } + else + { + return LMJ_EdgeInsets_; + } +} + +- (id<LMJVerticalFlowLayoutDelegate>)delegate +{ + return (id<LMJVerticalFlowLayoutDelegate>)self.collectionView.dataSource; +} + +- (instancetype)initWithDelegate:(id<LMJVerticalFlowLayoutDelegate>)delegate +{ + if (self = [super init]) { + } + return self; +} + + ++ (instancetype)flowLayoutWithDelegate:(id<LMJVerticalFlowLayoutDelegate>)delegate +{ + return [[self alloc] initWithDelegate:delegate]; +} + +@end -- Gitblit v1.8.0