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/LMJHorizontalFlowLayout.m | 219 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 219 insertions(+), 0 deletions(-) diff --git a/screendisplay/screendisplay/Classes/Base/BaseView/LMJHorizontalFlowLayout.m b/screendisplay/screendisplay/Classes/Base/BaseView/LMJHorizontalFlowLayout.m new file mode 100755 index 0000000..4029c6d --- /dev/null +++ b/screendisplay/screendisplay/Classes/Base/BaseView/LMJHorizontalFlowLayout.m @@ -0,0 +1,219 @@ +// +// LMJHorizontalFlowLayout.m +// ��������������������� +// +// Created by apple on 16/7/31. +// Copyright �� 2016��� NJHu. All rights reserved. +// + +#import "LMJHorizontalFlowLayout.h" + +#define LMJXX(x) floorf(x) +#define LMJXS(s) ceilf(s) + +static const NSInteger LMJ_Lines_ = 3; +static const CGFloat LMJ_XMargin_ = 10; +static const CGFloat LMJ_YMargin_ = 10; +static const UIEdgeInsets LMJ_EdgeInsets_ = {10, 10, 10, 10}; + +@interface LMJHorizontalFlowLayout() + +/** ���������cell���attrbts */ +@property (nonatomic, strong) NSMutableArray<UICollectionViewLayoutAttributes *> *lmj_AtrbsArray; + +/** ��������������������������� */ +@property (nonatomic, strong) NSMutableArray<NSNumber *> *lmj_LinesWidthArray; + +- (NSInteger)lines; + +- (CGFloat)xMarginAtIndexPath:(NSIndexPath *)indexPath; + +- (CGFloat)yMargin; + +- (UIEdgeInsets)edgeInsets; + +@end + +@implementation LMJHorizontalFlowLayout + + + +/** + * ������������������������������������ + */ +- (void)prepareLayout +{ + [super prepareLayout]; + + //������������������������������������������������������ + [self.lmj_LinesWidthArray removeAllObjects]; + + //���������������������������, ������������������ + for (NSInteger i = 0; i < self.lines; i++) { + [self.lmj_LinesWidthArray addObject:@(self.edgeInsets.left)]; + } + + // ���������������������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 h = 1.0 * (self.collectionView.frame.size.height - self.edgeInsets.top - self.edgeInsets.bottom - self.yMargin * (self.lines - 1)) / self.lines; + + h = LMJXX(h); + + // ���������������������, ������������������������������ + CGFloat w = [self.delegate waterflowLayout:self collectionView:self.collectionView widthForItemAtIndexPath:indexPath itemHeight:h]; + + // ���������������������������������������, ���������0��������� + __block NSInteger indexLine = 0; + __block CGFloat minLineW = [self.lmj_LinesWidthArray[indexLine] doubleValue]; + + [self.lmj_LinesWidthArray enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { + CGFloat lineW = obj.doubleValue; + if(minLineW > lineW) + { + minLineW = lineW; + indexLine = idx; + } + }]; + + + CGFloat x = [self xMarginAtIndexPath:indexPath] + minLineW; + + if (minLineW == self.edgeInsets.left) { + x = self.edgeInsets.left; + } + + CGFloat y = self.edgeInsets.top + (self.yMargin + h) * indexLine; + + // ������frame + atrbs.frame = CGRectMake(x, y, w, h); + + // ���������������������������;��������������� + self.lmj_LinesWidthArray[indexLine] = @(CGRectGetMaxX(atrbs.frame)); + + return atrbs; +} + + +- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect +{ + return self.lmj_AtrbsArray; +} + + +- (CGSize)collectionViewContentSize +{ + __block CGFloat maxColW = [self.lmj_LinesWidthArray[0] doubleValue]; + + [self.lmj_LinesWidthArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { + + if (maxColW < [obj doubleValue]) { + maxColW = [obj doubleValue]; + } + + }]; + + return CGSizeMake(maxColW + self.edgeInsets.right, self.collectionView.frame.size.height); +} + + +- (NSMutableArray *)lmj_AtrbsArray +{ + if(_lmj_AtrbsArray == nil) + { + _lmj_AtrbsArray = [NSMutableArray array]; + } + return _lmj_AtrbsArray; +} + +- (NSMutableArray *)lmj_LinesWidthArray +{ + if(_lmj_LinesWidthArray == nil) + { + _lmj_LinesWidthArray = [NSMutableArray array]; + } + return _lmj_LinesWidthArray; +} + +- (NSInteger)lines +{ + if([self.delegate respondsToSelector:@selector(waterflowLayout:linesInCollectionView:)]) + { + return [self.delegate waterflowLayout:self linesInCollectionView:self.collectionView]; + } + else + { + return LMJ_Lines_; + } +} + +- (CGFloat)xMarginAtIndexPath:(NSIndexPath *)indexPath +{ + if([self.delegate respondsToSelector:@selector(waterflowLayout:collectionView:columnsMarginForItemAtIndexPath:)]) + { + return [self.delegate waterflowLayout:self collectionView:self.collectionView columnsMarginForItemAtIndexPath:indexPath]; + } + else + { + return LMJ_XMargin_; + } +} + +- (CGFloat)yMargin +{ + if([self.delegate respondsToSelector:@selector(waterflowLayout:linesMarginInCollectionView:)]) + { + return [self.delegate waterflowLayout:self linesMarginInCollectionView:self.collectionView]; + }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<LMJHorizontalFlowLayoutDelegate>)delegate +{ + return (id<LMJHorizontalFlowLayoutDelegate>)self.collectionView.dataSource; +} + +- (instancetype)initWithDelegate:(id<LMJHorizontalFlowLayoutDelegate>)delegate +{ + if (self = [super init]) { + + } + return self; +} + + ++ (instancetype)flowLayoutWithDelegate:(id<LMJHorizontalFlowLayoutDelegate>)delegate +{ + return [[self alloc] initWithDelegate:delegate]; +} + +@end -- Gitblit v1.8.0