Files
keyboard/keyBoard/Class/Pay/V/KBSvipFlowLayout.m

138 lines
5.6 KiB
Mathematica
Raw Normal View History

2026-02-04 12:48:18 +08:00
//
// KBSvipFlowLayout.m
// keyBoard
//
2026-02-04 15:09:03 +08:00
// SVIP Section
2026-02-04 12:48:18 +08:00
//
#import "KBSvipFlowLayout.h"
#import "KBSvipBenefitBgView.h"
2026-02-04 15:09:03 +08:00
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
2026-02-04 12:48:18 +08:00
@interface KBSvipFlowLayout ()
@property (nonatomic, strong) NSMutableArray<UICollectionViewLayoutAttributes *> *decorationAttributes;
@end
@implementation KBSvipFlowLayout
- (instancetype)init {
if (self = [super init]) {
2026-02-04 15:09:03 +08:00
_decorationSection = 1; // Section 1
[self registerClass:[KBSvipOuterBgView class] forDecorationViewOfKind:kKBSvipOuterBgKind];
[self registerClass:[KBSvipBenefitBgView class] forDecorationViewOfKind:kKBSvipBenefitBgKind];
2026-02-04 12:48:18 +08:00
}
return self;
}
- (void)prepareLayout {
[super prepareLayout];
self.decorationAttributes = [NSMutableArray array];
NSInteger numberOfSections = [self.collectionView numberOfSections];
2026-02-04 15:09:03 +08:00
if (numberOfSections < 2) { return; }
NSInteger itemCount0 = [self.collectionView numberOfItemsInSection:0];
NSInteger itemCount1 = [self.collectionView numberOfItemsInSection:1];
if (itemCount0 == 0 || itemCount1 == 0) { return; }
2026-02-04 12:48:18 +08:00
2026-02-04 15:09:03 +08:00
// 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]];
2026-02-04 12:48:18 +08:00
2026-02-04 15:09:03 +08:00
if (!firstItemAttr || !benefitHeaderAttr || !lastBenefitAttr) { return; }
2026-02-04 12:48:18 +08:00
2026-02-04 15:09:03 +08:00
UIEdgeInsets sectionInset0 = [self insetForSection:0];
UIEdgeInsets sectionInset1 = [self insetForSection:1];
2026-02-04 12:48:18 +08:00
2026-02-04 15:40:45 +08:00
// Section inset
CGFloat outerPadding = 16;
// 1. 18 16
2026-02-04 15:09:03 +08:00
{
CGFloat minY = CGRectGetMinY(firstItemAttr.frame) - sectionInset0.top;
CGFloat maxY = CGRectGetMaxY(lastBenefitAttr.frame) + 16;
2026-02-04 15:40:45 +08:00
CGFloat x = outerPadding;
CGFloat width = self.collectionView.bounds.size.width - outerPadding * 2;
2026-02-04 15:09:03 +08:00
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];
}
2026-02-04 12:48:18 +08:00
2026-02-04 15:09:03 +08:00
// 2. 15 Section 1 Header Section 1 8px
{
2026-02-04 15:40:45 +08:00
CGFloat innerPadding = 8; //
2026-02-04 15:09:03 +08:00
CGFloat minY = CGRectGetMinY(benefitHeaderAttr.frame);
CGFloat maxY = CGRectGetMaxY(lastBenefitAttr.frame) + 16;
2026-02-04 15:40:45 +08:00
CGFloat x = outerPadding + innerPadding;
CGFloat width = self.collectionView.bounds.size.width - (outerPadding + innerPadding) * 2;
2026-02-04 15:09:03 +08:00
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 {
2026-02-04 12:48:18 +08:00
UIEdgeInsets sectionInset = self.sectionInset;
if ([self.collectionView.delegate respondsToSelector:@selector(collectionView:layout:insetForSectionAtIndex:)]) {
2026-02-04 15:09:03 +08:00
sectionInset = [(id<UICollectionViewDelegateFlowLayout>)self.collectionView.delegate collectionView:self.collectionView layout:self insetForSectionAtIndex:section];
2026-02-04 12:48:18 +08:00
}
2026-02-04 15:09:03 +08:00
return sectionInset;
2026-02-04 12:48:18 +08:00
}
- (NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
NSMutableArray *attrs = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
for (UICollectionViewLayoutAttributes *decorationAttr in self.decorationAttributes) {
if (CGRectIntersectsRect(rect, decorationAttr.frame)) {
[attrs addObject:decorationAttr];
}
}
return attrs;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath {
2026-02-04 15:09:03 +08:00
for (UICollectionViewLayoutAttributes *attr in self.decorationAttributes) {
if ([attr.representedElementKind isEqualToString:elementKind] && attr.indexPath.section == indexPath.section) {
return attr;
2026-02-04 12:48:18 +08:00
}
}
return [super layoutAttributesForDecorationViewOfKind:elementKind atIndexPath:indexPath];
}
@end