美文网首页
DecorationView

DecorationView

作者: 牧_e50d | 来源:发表于2018-11-21 11:06 被阅读0次
    #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
    

    相关文章

      网友评论

          本文标题:DecorationView

          本文链接:https://www.haomeiwen.com/subject/laffqqtx.html