This commit is contained in:
2026-02-04 15:09:03 +08:00
parent e50eaecbd9
commit faae0297cb
3 changed files with 81 additions and 35 deletions

View File

@@ -12,6 +12,8 @@
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor colorWithHex:0xF6F7FB];
// self.backgroundColor = [UIColor redColor];
self.layer.cornerRadius = 15;
self.layer.masksToBounds = YES;
}

View File

@@ -2,13 +2,34 @@
// KBSvipFlowLayout.m
// keyBoard
//
// SVIP Section
// SVIP Section
//
#import "KBSvipFlowLayout.h"
#import "KBSvipBenefitBgView.h"
static NSString * const kKBSvipDecorationViewKind = @"KBSvipBenefitBgDecoration";
static NSString * const kKBSvipOuterBgKind = @"KBSvipOuterBgDecoration"; // 18
static NSString * const kKBSvipBenefitBgKind = @"KBSvipBenefitBgDecoration"; // 15
#pragma mark - 18+
@interface KBSvipOuterBgView : UICollectionReusableView
@end
@implementation KBSvipOuterBgView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor whiteColor];
self.layer.cornerRadius = 18;
self.layer.masksToBounds = YES;
}
return self;
}
@end
#pragma mark - KBSvipFlowLayout
@interface KBSvipFlowLayout ()
@property (nonatomic, strong) NSMutableArray<UICollectionViewLayoutAttributes *> *decorationAttributes;
@@ -18,8 +39,9 @@ static NSString * const kKBSvipDecorationViewKind = @"KBSvipBenefitBgDecoration"
- (instancetype)init {
if (self = [super init]) {
_decorationSection = 1; // Section 1
[self registerClass:[KBSvipBenefitBgView class] forDecorationViewOfKind:kKBSvipDecorationViewKind];
_decorationSection = 1; // Section 1
[self registerClass:[KBSvipOuterBgView class] forDecorationViewOfKind:kKBSvipOuterBgKind];
[self registerClass:[KBSvipBenefitBgView class] forDecorationViewOfKind:kKBSvipBenefitBgKind];
}
return self;
}
@@ -30,38 +52,61 @@ static NSString * const kKBSvipDecorationViewKind = @"KBSvipBenefitBgDecoration"
self.decorationAttributes = [NSMutableArray array];
NSInteger numberOfSections = [self.collectionView numberOfSections];
if (self.decorationSection >= numberOfSections) { return; }
if (numberOfSections < 2) { return; }
NSInteger numberOfItems = [self.collectionView numberOfItemsInSection:self.decorationSection];
if (numberOfItems == 0) { return; }
NSInteger itemCount0 = [self.collectionView numberOfItemsInSection:0];
NSInteger itemCount1 = [self.collectionView numberOfItemsInSection:1];
if (itemCount0 == 0 || itemCount1 == 0) { return; }
// Section Header
UICollectionViewLayoutAttributes *headerAttr = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:[NSIndexPath indexPathForItem:0 inSection:self.decorationSection]];
// Section 0 item
UICollectionViewLayoutAttributes *firstItemAttr = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
// Section 1 Header
UICollectionViewLayoutAttributes *benefitHeaderAttr = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]];
// Section 1 item
UICollectionViewLayoutAttributes *lastBenefitAttr = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:itemCount1 - 1 inSection:1]];
// item
UICollectionViewLayoutAttributes *firstItemAttr = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:self.decorationSection]];
UICollectionViewLayoutAttributes *lastItemAttr = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:numberOfItems - 1 inSection:self.decorationSection]];
if (!firstItemAttr || !benefitHeaderAttr || !lastBenefitAttr) { return; }
if (!firstItemAttr || !lastItemAttr) { return; }
UIEdgeInsets sectionInset0 = [self insetForSection:0];
UIEdgeInsets sectionInset1 = [self insetForSection:1];
//
UIEdgeInsets sectionInset = self.sectionInset;
if ([self.collectionView.delegate respondsToSelector:@selector(collectionView:layout:insetForSectionAtIndex:)]) {
sectionInset = [(id<UICollectionViewDelegateFlowLayout>)self.collectionView.delegate collectionView:self.collectionView layout:self insetForSectionAtIndex:self.decorationSection];
// 1. 18 Section 0 Section 1
{
CGFloat minY = CGRectGetMinY(firstItemAttr.frame) - sectionInset0.top;
CGFloat maxY = CGRectGetMaxY(lastBenefitAttr.frame) + 16;
CGFloat x = sectionInset0.left;
CGFloat width = self.collectionView.bounds.size.width - sectionInset0.left - sectionInset0.right;
CGRect frame = CGRectMake(x, minY, width, maxY - minY);
UICollectionViewLayoutAttributes *attr = [UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:kKBSvipOuterBgKind withIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
attr.frame = frame;
attr.zIndex = -2; //
[self.decorationAttributes addObject:attr];
}
CGFloat minY = CGRectGetMinY(headerAttr.frame);
CGFloat maxY = CGRectGetMaxY(lastItemAttr.frame) + 16; // 16
CGFloat x = sectionInset.left;
CGFloat width = self.collectionView.bounds.size.width - sectionInset.left - sectionInset.right;
CGRect decorationFrame = CGRectMake(x, minY, width, maxY - minY);
UICollectionViewLayoutAttributes *decorationAttr = [UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:kKBSvipDecorationViewKind withIndexPath:[NSIndexPath indexPathForItem:0 inSection:self.decorationSection]];
decorationAttr.frame = decorationFrame;
decorationAttr.zIndex = -1; //
[self.decorationAttributes addObject:decorationAttr];
// 2. 15 Section 1 Header Section 1 8px
{
CGFloat minY = CGRectGetMinY(benefitHeaderAttr.frame);
CGFloat maxY = CGRectGetMaxY(lastBenefitAttr.frame) + 16;
CGFloat x = sectionInset0.left + 8; // 8px
CGFloat width = self.collectionView.bounds.size.width - sectionInset0.left - sectionInset0.right - 16; // 8px
CGRect frame = CGRectMake(x, minY, width, maxY - minY);
UICollectionViewLayoutAttributes *attr = [UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:kKBSvipBenefitBgKind withIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]];
attr.frame = frame;
attr.zIndex = -1; //
[self.decorationAttributes addObject:attr];
}
}
- (UIEdgeInsets)insetForSection:(NSInteger)section {
UIEdgeInsets sectionInset = self.sectionInset;
if ([self.collectionView.delegate respondsToSelector:@selector(collectionView:layout:insetForSectionAtIndex:)]) {
sectionInset = [(id<UICollectionViewDelegateFlowLayout>)self.collectionView.delegate collectionView:self.collectionView layout:self insetForSectionAtIndex:section];
}
return sectionInset;
}
- (NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
@@ -77,11 +122,9 @@ static NSString * const kKBSvipDecorationViewKind = @"KBSvipBenefitBgDecoration"
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath {
if ([elementKind isEqualToString:kKBSvipDecorationViewKind]) {
for (UICollectionViewLayoutAttributes *attr in self.decorationAttributes) {
if (attr.indexPath.section == indexPath.section) {
return attr;
}
for (UICollectionViewLayoutAttributes *attr in self.decorationAttributes) {
if ([attr.representedElementKind isEqualToString:elementKind] && attr.indexPath.section == indexPath.section) {
return attr;
}
}
return [super layoutAttributesForDecorationViewOfKind:elementKind atIndexPath:indexPath];

View File

@@ -78,7 +78,8 @@ static NSString * const kKBSvipBenefitHeaderId = @"kKBSvipBenefitHeaderId";
}];
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.top.equalTo(self.view);
make.left.right.equalTo(self.view);
make.top.equalTo(self.view).offset(16);
make.bottom.equalTo(self.payButton.mas_top).offset(-16);
}];
}