// // WMZBannerFlowLayout.m // WMZBanner // // Created by wmz on 2019/9/6. // Copyright © 2019 wmz. All rights reserved. // #import "WMZBannerFlowLayout.h" @interface WMZBannerFlowLayout(){ CGSize factItemSize; } @end @implementation WMZBannerFlowLayout - (instancetype)initConfigureWithModel:(WMZBannerParam *)param{ if (self = [super init]) { self.param = param; } return self; } - (void)prepareLayout { [super prepareLayout]; self.itemSize = self.param.wItemSize; self.minimumInteritemSpacing = (self.param.wFrame.size.height-self.param.wItemSize.height)/2; self.minimumLineSpacing = self.param.wLineSpacing; self.sectionInset = self.param.wSectionInset; if ([self.collectionView isPagingEnabled]) { self.scrollDirection = self.param.wVertical? UICollectionViewScrollDirectionVertical :UICollectionViewScrollDirectionHorizontal; }else{ self.scrollDirection = UICollectionViewScrollDirectionHorizontal; } } - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { return [self cardScaleTypeInRect:rect]; } //卡片缩放 - (NSArray *)cardScaleTypeInRect:(CGRect)rect{ if (!self.param.wCardOverLap) { self.param.myCurrentPath = round((ABS(self.collectionView.contentOffset.x))/(self.param.wItemSize.width+self.param.wLineSpacing)); } NSArray *array = [self getCopyOfAttributes:[super layoutAttributesForElementsInRect:rect]]; if (!self.param.wScale||self.param.wMarquee) { return array; } CGRect visibleRect = CGRectZero; visibleRect.origin = self.collectionView.contentOffset; visibleRect.size = self.collectionView.bounds.size; for (int i = 0; i * _Nullable bindings){ return (evaluatedObject.representedElementCategory == UICollectionElementCategoryCell); }]; NSArray *cellAttributes = [attributes filteredArrayUsingPredicate: cellAttributesPredicate]; UICollectionViewLayoutAttributes *currentAttributes; for (UICollectionViewLayoutAttributes *layoutAttributes in cellAttributes) { CGFloat itemHorizontalCenter = layoutAttributes.center.x; if (ABS(itemHorizontalCenter - horizontalCenter) < ABS(offSetAdjustment)) { currentAttributes = layoutAttributes; offSetAdjustment = itemHorizontalCenter - horizontalCenter; } } CGFloat nextOffset = proposedContentOffset.x + offSetAdjustment; proposedContentOffset.x = nextOffset; CGFloat deltaX = proposedContentOffset.x - self.collectionView.contentOffset.x; CGFloat velX = velocity.x; if (fabs(deltaX) <= FLT_EPSILON || fabs(velX) <= FLT_EPSILON || (velX > 0.0 && deltaX > 0.0) || (velX < 0.0 && deltaX < 0.0)) { }else if (velocity.x > 0.0){ NSArray *revertedArray = [[attributes reverseObjectEnumerator] allObjects]; BOOL found = YES; float proposedX = 0.0; for (UICollectionViewLayoutAttributes *layoutAttributes in revertedArray) { if(layoutAttributes.representedElementCategory == UICollectionElementCategoryCell) { CGFloat itemHorizontalCenter = layoutAttributes.center.x; if (itemHorizontalCenter > proposedContentOffset.x) { found = YES; proposedX = nextOffset + (currentAttributes.frame.size.width / 2) + (layoutAttributes.frame.size.width / 2); } else { break; } } } if (found) { proposedContentOffset.x = proposedX; proposedContentOffset.x += self.param.wLineSpacing; } } else if (velocity.x < 0.0) { for (UICollectionViewLayoutAttributes *layoutAttributes in cellAttributes) { CGFloat itemHorizontalCenter = layoutAttributes.center.x; if (itemHorizontalCenter > proposedContentOffset.x) { proposedContentOffset.x = nextOffset - ((currentAttributes.frame.size.width / 2) + (layoutAttributes.frame.size.width / 2)); proposedContentOffset.x -= self.param.wLineSpacing; break; } } } proposedContentOffset.y = 0.0; return proposedContentOffset; } @end