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/LMJElementsFlowLayout.m | 207 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 207 insertions(+), 0 deletions(-) diff --git a/screendisplay/screendisplay/Classes/Base/BaseView/LMJElementsFlowLayout.m b/screendisplay/screendisplay/Classes/Base/BaseView/LMJElementsFlowLayout.m new file mode 100755 index 0000000..eaf43de --- /dev/null +++ b/screendisplay/screendisplay/Classes/Base/BaseView/LMJElementsFlowLayout.m @@ -0,0 +1,207 @@ +// +// LMJElementsFlowLayout.m +// +// Created by apple on 17/4/19. +// Copyright �� 2017��� NJHu. All rights reserved. +// +#import "LMJElementsFlowLayout.h" + +#define LMJXX(x) floorf(x) +#define LMJXS(s) ceilf(s) + +static const CGFloat LMJ_XMargin_ = 10; +static const CGFloat LMJ_YMargin_ = 10; +static const UIEdgeInsets LMJ_EdgeInsets_ = {20, 10, 10, 10}; + +@interface LMJElementsFlowLayout() + +/** ���������cell���attrbts */ +@property (nonatomic, strong) NSMutableArray<UICollectionViewLayoutAttributes *> *lmj_AtrbsArray; + +/** ��������������������������� */ +@property (assign, nonatomic) CGRect lmj_LastAtrbsFrame; + + +- (CGFloat)xMarginAtIndexPath:(NSIndexPath *)indexPath; + +- (CGFloat)yMarginAtIndexPath:(NSIndexPath *)indexPath; + +- (UIEdgeInsets)edgeInsets; + +- (CGRect)maxHeightFrame; + +@end + +@implementation LMJElementsFlowLayout + + + +/** + * ������������������������������������ + */ +- (void)prepareLayout +{ + [super prepareLayout]; + + //������������������������������������������������������ + //���������������������������, ������������������ + self.lmj_LastAtrbsFrame = CGRectMake(0, 0, self.collectionView.frame.size.width, 0); + + // ���������������������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]; + + // ��������� + CGSize itemSize = [self.delegate waterflowLayout:self collectionView:self.collectionView sizeForItemAtIndexPath:indexPath]; + + CGFloat w = LMJXX(itemSize.width); + w = MIN(w, self.collectionView.frame.size.width); + + // ���������������������, ������������������������������ + CGFloat h = itemSize.height; + + // ���������������������������������������, ���������0��������� + CGFloat rightLeftWidth = self.collectionView.frame.size.width - CGRectGetMaxX(self.lmj_LastAtrbsFrame) - [self xMarginAtIndexPath:indexPath] - self.edgeInsets.right; + + CGFloat x = self.edgeInsets.left; + CGFloat y = self.edgeInsets.top; + + + if (rightLeftWidth >= w) { + + x = CGRectGetMaxX(self.lmj_LastAtrbsFrame) + [self xMarginAtIndexPath:indexPath]; + y = self.lmj_LastAtrbsFrame.origin.y; + + }else + { + x = self.edgeInsets.left; + y = CGRectGetMaxY(self.maxHeightFrame) + [self yMarginAtIndexPath:indexPath]; + } + + if (w > self.collectionView.frame.size.width - self.edgeInsets.left - self.edgeInsets.right) { + x = (self.collectionView.frame.size.width - w) * 0.5; + } + + if (y <= [self yMarginAtIndexPath:indexPath]) { + y = self.edgeInsets.top; + } + + // ������frame + atrbs.frame = CGRectMake(x, y, w, h); + + // ���������������������������;��������������� + self.lmj_LastAtrbsFrame = atrbs.frame; + + return atrbs; +} + + +- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect +{ + return self.lmj_AtrbsArray; +} + + +- (CGRect)maxHeightFrame +{ + __block CGRect maxHeightFrame = self.lmj_LastAtrbsFrame; + + [self.lmj_AtrbsArray enumerateObjectsUsingBlock:^(UICollectionViewLayoutAttributes * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { + + if (CGRectGetMaxY(obj.frame) > CGRectGetMaxY(maxHeightFrame)) { + maxHeightFrame = obj.frame; + } + }]; + + return maxHeightFrame; +} + + + +- (CGSize)collectionViewContentSize +{ + + return CGSizeMake(self.collectionView.frame.size.width, CGRectGetMaxY(self.maxHeightFrame) + self.edgeInsets.bottom); +} + + +- (NSMutableArray *)lmj_AtrbsArray +{ + if(_lmj_AtrbsArray == nil) + { + _lmj_AtrbsArray = [NSMutableArray array]; + } + return _lmj_AtrbsArray; +} + + +- (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)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<LMJElementsFlowLayoutDelegate>)delegate +{ + return (id<LMJElementsFlowLayoutDelegate>)self.collectionView.dataSource; +} + +- (instancetype)initWithDelegate:(id<LMJElementsFlowLayoutDelegate>)delegate +{ + if (self = [super init]) { + + } + return self; +} + + ++ (instancetype)flowLayoutWithDelegate:(id<LMJElementsFlowLayoutDelegate>)delegate +{ + return [[self alloc] initWithDelegate:delegate]; +} + + +@end -- Gitblit v1.8.0