美文网首页人猿星球
ios- 自定义collection 背景图

ios- 自定义collection 背景图

作者: qui丶MyLove | 来源:发表于2017-06-30 17:55 被阅读50次

效果:

运行效果

1.首先要先继承UICollectionReusableView 的一个view ,collectionview背景图是UICollectionReusableView子类;里面实现自定义效果如下:

自定义背景图

2.自定义collectionLayout关键在于layout中的实现计算frame

#importstatic NSString * const DZSetSectionBack = @"ccDecoration";@protocol DZSetSectionBackgroundLayoutDelegate@required-(UIEdgeInsets)cccollectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;-(CGSize)cccollectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout headerForSectionAtIndex:(NSInteger)section;@optional-(CGSize)cccollectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout footerForSectionAtIndex:(NSInteger)section;@end@interface DZSetSectionBackground : UICollectionViewFlowLayout@property (nonatomic, assign)idmydelegate;

@end

#import "DZSetSectionBackground.h"

#import "DZMyBackReusableView.h"

#import "Header.h"

@interface DZSetSectionBackground ()

/**

存储 布局的 att

*/

@property (nonatomic,strong) NSMutableArray *layoutInfoArr;

/**

collection 滑动区域大小

*/

@property (nonatomic,assign) CGSize contentSize;

@end

@implementation DZSetSectionBackground

/**

布局 布局配置数据  布局前的准备会调用这个方法

*/

- (void)prepareLayout{

[super prepareLayout];

//获取布局信息

[self registerClass:[DZMyBackReusableView class] forDecorationViewOfKind:DZSetSectionBack];

[self.layoutInfoArr removeAllObjects];

CGRect tmpsectionFrame = CGRectZero;

NSInteger numberOfSections = [self.collectionView numberOfSections];

for (NSInteger section = 0; section < numberOfSections; section++){

NSInteger num = [self.collectionView numberOfItemsInSection:section];

UICollectionViewLayoutAttributes *first = nil;//组第一个 item att

UICollectionViewLayoutAttributes *last = nil;//组最后一个 item att

if (num>0) {

first = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];

last = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:num-1 inSection:section]];

}

UIEdgeInsets sectionInset = self.sectionInset;

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

if (!UIEdgeInsetsEqualToEdgeInsets(inset, UIEdgeInsetsZero)) {

sectionInset = inset;

}

CGSize headerSize = [self.mydelegate cccollectionView:self.collectionView layout:self headerForSectionAtIndex:section];

//        CGSize footerSize = [self.mydelegate cccollectionView:self.collectionView layout:self footerForSectionAtIndex:section];

CGRect sectionFrame = CGRectZero;

if (self.scrollDirection == UICollectionViewScrollDirectionHorizontal) {

sectionFrame = CGRectMake(CGRectGetMinX(first.frame)-headerSize.width+sectionInset.left, CGRectGetMinY(first.frame)+sectionInset.top, CGRectGetMaxX(last.frame)-sectionInset.right, CGRectGetMaxY(last.frame)-sectionInset.bottom);

sectionFrame.origin.y = sectionInset.top;

sectionFrame.size.width = sectionFrame.size.width-sectionFrame.origin.x;

sectionFrame.size.height = CGRectGetHeight(self.collectionView.frame)-sectionInset.top-sectionInset.bottom;

} else {

sectionFrame = CGRectMake(CGRectGetMinX(first.frame), CGRectGetMinY(first.frame)-headerSize.height+sectionInset.top, CGRectGetMaxX(last.frame), CGRectGetMaxY(last.frame)-sectionInset.bottom);

sectionFrame.origin.x = sectionInset.left;

sectionFrame.size.width = CGRectGetWidth(self.collectionView.frame)-sectionInset.left-sectionInset.right;

sectionFrame.size.height = sectionFrame.size.height-sectionFrame.origin.y;

}

UICollectionViewLayoutAttributes *att = [self layoutAttributesForDecorationViewOfKind:DZSetSectionBack atIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];

att.frame = sectionFrame;

att.zIndex = -1;

att.hidden = NO;

[self.layoutInfoArr addObject:att];

tmpsectionFrame = sectionFrame;

}

}

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{

NSArray *arr = [super layoutAttributesForElementsInRect:rect];

NSMutableArray *rt = [NSMutableArray array];

[rt addObjectsFromArray:arr];

for (UICollectionViewLayoutAttributes *att in self.layoutInfoArr) {

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

[rt addObject:att];

}

}

return rt;

}

-(NSMutableArray *)layoutInfoArr{

if (_layoutInfoArr==nil) {

_layoutInfoArr = [NSMutableArray array];

}

return _layoutInfoArr;

}

-(UICollectionViewLayoutAttributes*)layoutAttributesForDecorationViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath{

UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:elementKind withIndexPath:indexPath];

return attrs;

}

@end

3.demo下载:github.com/C100515C/CollectionCustomBackgroud.git

感觉可以给个赞!!!

相关文章

网友评论

  • _Andy_:用 markdown 编辑就好看了

本文标题:ios- 自定义collection 背景图

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