DecorationView

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface CGCSectionLayout : UICollectionViewFlowLayout

@end

NS_ASSUME_NONNULL_END

#import "CGCSectionLayout.h"
#import "CGCSectionBackgroundView.h"
#import "CGCSectionABackground.h"
#import "CGCSectionBBackground.h"
#import "CGCSectionCBackground.h"

NSString *decorationViewOfKind = @"decorationBgView";

@interface CGCSectionLayout ()
@property (nonatomic, strong) NSMutableArray *itemsAttribute;
@end

@implementation CGCSectionLayout

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self registerClass:[CGCSectionBackgroundView class] forDecorationViewOfKind:decorationViewOfKind];
        [self registerClass:[CGCSectionABackground class] forDecorationViewOfKind:@"CGCSectionABackground"];
        [self registerClass:[CGCSectionBBackground class] forDecorationViewOfKind:@"CGCSectionBBackground"];
    }
    return self;
}

-(void)prepareLayout{
    [super prepareLayout];
    NSLog(@"prepareLayout..:%@ ",[NSDate date]);
    self.itemsAttribute=[NSMutableArray new];
    NSInteger numberOfSection=self.collectionView.numberOfSections;
    for (int section=0; section<numberOfSection; section++) {
        //获取当前section的最后一个item index
        {
            NSInteger itemCounts=[self.collectionView numberOfItemsInSection:section];
            NSInteger line=itemCounts/3;
            if (itemCounts%3!=0) {
                line++;
            }
            for (int i=0; i<line; i++) {
                UICollectionViewLayoutAttributes *attribute;
                if (i%3==0) {
                    attribute=[UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:decorationViewOfKind withIndexPath:[NSIndexPath indexPathForItem:i inSection:section]];
                }else if (i%3==1){
                    attribute=[UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:@"CGCSectionABackground" withIndexPath:[NSIndexPath indexPathForItem:i inSection:section]];
                }else if (i%3==2){
                    attribute=[UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:@"CGCSectionBBackground" withIndexPath:[NSIndexPath indexPathForItem:i inSection:section]];
                }
                attribute.zIndex=-1;
                UICollectionViewLayoutAttributes *firstItem=[self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i*3 inSection:section]];
                CGFloat tempOrigin=tempOrigin=firstItem.frame.origin.y;
                CGFloat tempHeight=firstItem.frame.size.height;
                if (i==0) {
                    tempOrigin-=self.sectionInset.top;
                    tempHeight+=self.sectionInset.top+self.minimumLineSpacing*0.5;
                }else{
                    tempOrigin-=self.minimumLineSpacing*0.5;
                    tempHeight+=self.minimumLineSpacing;
                }
                attribute.frame=CGRectMake(0, tempOrigin, self.collectionView.frame.size.width, tempHeight);
                 [self.itemsAttribute addObject:attribute];
            }
        }
    }
}

-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
    NSMutableArray *attributes=[NSMutableArray arrayWithArray:[super layoutAttributesForElementsInRect:rect]];
    for (UICollectionViewLayoutAttributes *attribute in self.itemsAttribute) {
        if (CGRectIntersectsRect(rect, attribute.frame)) {
            [attributes addObject:attribute];
        }
    }
    return attributes;
}


@end
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容