美文网首页
2021-08-10

2021-08-10

作者: 马克雅 | 来源:发表于2021-08-10 18:29 被阅读0次

#import

/// 扩展section的背景色

@protocol ULBCollectionViewDelegateFlowLayout <UICollectionViewDelegateFlowLayout>

- (UIColor*)collectionView:(UICollectionView*)collectionViewlayout:(UICollectionViewLayout*)collectionViewLayoutcolorForSectionAtIndex:(NSInteger)section;

@end

@interface ULBCollectionViewFlowLayout : UICollectionViewFlowLayout

@end

////////

#import "ULBCollectionViewFlowLayout.h"

static NSString *const ULBCollectionViewSectionColor = @"com.ulb.ULBCollectionElementKindSectionColor";

@interface ULBCollectionViewLayoutAttributes  :UICollectionViewLayoutAttributes

// 背景色

@property (nonatomic, strong) UIColor *backgroudColor;

@end

@implementation ULBCollectionViewLayoutAttributes

@end

@interface ULBCollectionReusableView : UICollectionReusableView

@end

@implementation ULBCollectionReusableView

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{

    [superapplyLayoutAttributes:layoutAttributes];

    ULBCollectionViewLayoutAttributes *attr = (ULBCollectionViewLayoutAttributes *)layoutAttributes;

    self.backgroundColor = attr.backgroudColor;

}

@end

@interface ULBCollectionViewFlowLayout  ()

@property (nonatomic, strong) UIColor *sectonColor;

@property (nonatomic, strong) NSMutableArray<UICollectionViewLayoutAttributes *> *decorationViewAttrs;

@end

@implementation ULBCollectionViewFlowLayout

- (void)prepareLayout{

    [super prepareLayout];

    NSInteger sections = [self.collectionView numberOfSections];

    id<ULBCollectionViewDelegateFlowLayout> delegate  =self.collectionView.delegate;

    if([delegaterespondsToSelector:@selector(collectionView:layout:colorForSectionAtIndex:)]) {

    }else{

        return;

    }

    //1.初始化

    [self registerClass:[ULBCollectionReusableView class] forDecorationViewOfKind:ULBCollectionViewSectionColor];

    [self.decorationViewAttrs removeAllObjects];

    for(NSIntegersection =0; section < sections; section++) {

        NSIntegernumberOfItems = [self.collectionViewnumberOfItemsInSection:section];

        if(numberOfItems >0) {

            UICollectionViewLayoutAttributes *firstAttr = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];

            UICollectionViewLayoutAttributes*lastAttr = [selflayoutAttributesForItemAtIndexPath:[NSIndexPathindexPathForRow:(numberOfItems -1)inSection:section]];

            UIEdgeInsetssectionInset =self.sectionInset;

            if([delegaterespondsToSelector:@selector(collectionView:layout:insetForSectionAtIndex:)]) {

                UIEdgeInsets inset = [delegate collectionView:self.collectionView layout:self insetForSectionAtIndex:section];

                if(!UIEdgeInsetsEqualToEdgeInsets(inset, sectionInset)) {

                    sectionInset = inset;

                }

            }

            CGRectsectionFrame =CGRectUnion(firstAttr.frame, lastAttr.frame);

            sectionFrame.origin.x-= sectionInset.left;

            sectionFrame.origin.y-= sectionInset.top;

            if (self.scrollDirection == UICollectionViewScrollDirectionHorizontal) {

                sectionFrame.size.width+= sectionInset.left+ sectionInset.right;

                sectionFrame.size.height = self.collectionView.frame.size.height;

            }else{

                sectionFrame.size.width=self.collectionView.frame.size.width;

                sectionFrame.size.height+= sectionInset.top+ sectionInset.bottom;

            }

            //2. 定义

            ULBCollectionViewLayoutAttributes *attr = [ULBCollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:ULBCollectionViewSectionColor withIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];

            attr.frame= sectionFrame;

            attr.zIndex= -1;

            attr.backgroudColor = [delegate collectionView:self.collectionView layout:self colorForSectionAtIndex:section];

            [self.decorationViewAttrsaddObject:attr];

        }else{

            continue;

        }

    }

}

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{

    NSMutableArray * attrs = [[super layoutAttributesForElementsInRect:rect] mutableCopy];

    for (UICollectionViewLayoutAttributes *attr in self.decorationViewAttrs) {

        if(CGRectIntersectsRect(rect, attr.frame)) {

            [attrsaddObject:attr];

        }

    }

    return[attrscopy];

}

- (NSMutableArray<UICollectionViewLayoutAttributes *> *)decorationViewAttrs{

    if (!_decorationViewAttrs) {

        _decorationViewAttrs = [NSMutableArray array];

    }

    return _decorationViewAttrs;

}

@end

相关文章

  • 413. 等差数列划分

    2021-08-10 LeetCode每日一题 链接:https://leetcode-cn.com/proble...

  • 阅读打卡

    【日期】2021-08-10 【阅读书籍】山下英子《断舍离》 【阅读进度】63%—67% 【打卡天数】打卡24天 ...

  • 8.10--19

    Dear: “低小下”的嘲風先生草堂 微信群上的聊天记录如下,请查收。 ————— 2021-08-10 ————...

  • 人际关系是你进我退,我退你进

    日记894篇 2021-08-10 工业时代,信息泛滥,越是这样,越要专注,拒绝一切诱惑,聚焦一个点深挖。 例如你...

  • 交流:苹果近期固定批发的Other大礼包

    最近提交的包在2021-08-10早上6:00到6:30遭遇了苹果的Other大礼包。本来以为是我自己的个案,就没...

  • #Dairy279 无题

    2021-08-10 阴雨 周二 没有什么学习效率,基本上没有复习什么电磁场的内容,还是有点怕电磁场的内容。看了考...

  • 最好的爱情

    幸福日志2021-08-10 周二阴雨 六十几的母亲要离开父亲给南京的弟弟带孩子,父亲再三嘱咐新媳妇,“一定要好好...

  • 成为孩子成长路上的拐棍

    2021-08-10 女儿昨天学游泳,昨天回到家表示再也不去学了,老师按着她的头往水下按,想要淹死她,还不停的往她...

  • 做个“旁观者”

    [2021-08-10] 前两天看到一个采访报道,是关于巩立姣的。标题叫《巩立姣,“女孩本来的样子”》 巩立姣以2...

  • 【餐饮100问】71、遇到特别烦人的同事怎么办?

    D301 2021-08-10 在工作中有没有遇到过,特别烦人的同事?不知道什么原因,总是做的一些事情让你无法理解...

网友评论

      本文标题:2021-08-10

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