您的位置:首页 > 其它

OC瀑布流可拓展headView

2016-06-17 15:03 351 查看
1.SWVideoCollectionView.h 将collectionView封装在SWVideoCollectionView类中。

#import <UIKit/UIKit.h>

@protocol SWVideoCollectionViewDelegate <NSObject>

/**

 *  点击cell方法

 */

- (void)videoCollectionView:(UIView *)view index:(NSInteger)index;

/**

 *  点击头像方法

 */

- (void)videoCollectionPhotoView:(UIView *)view index:(NSInteger)index;

@optional

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString
*)kind atIndexPath:(NSIndexPath *)indexPath;

- (void)scrollViewDidScroll:(UIView *)view scrollView:(UIScrollView *)scrollView
contentOffset:(CGPoint)contentOffset;

@end

@interface SWVideoCollectionView :UIView

- (instancetype)initWithFrame:(CGRect)frame headerReferenceSize:(CGSize)headerReferenceSize
headClass:(NSString *)headClass;

@property(nonatomic,weak)UICollectionView
*collectionView;

@property(nonatomic,strong)NSArray
*dataLists;

@property(assign)id<SWVideoCollectionViewDelegate>delegate;

@end

1.SWVideoCollectionView.m

#import "SWVideoCollectionView.h"

#import "SWImgCollectionFlowLayout.h"

#import "SWMainDetailCollectionViewCell.h"

#import "SWVideoListModel.h"

#define cellCount 2

@interface
SWVideoCollectionView ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,SWMainDetailCollectionViewCellDelegate>

@property(nonatomic,copy)NSString
*headClass;

@end

static NSString *mainDetailId =@"mainDetailId";

static NSString *headScrollId =@"headScrollId";

@implementation SWVideoCollectionView

- (instancetype)initWithFrame:(CGRect)frame headerReferenceSize:(CGSize)headerReferenceSize
headClass:(NSString *)headClass {

    if (self = [superinitWithFrame:frame])
{

        SWImgCollectionFlowLayout *layOut = [[SWImgCollectionFlowLayoutalloc]
init]
1ea4f
;

        if (headerReferenceSize.height !=0 && headerReferenceSize.width
!=0) {

            layOut.headerReferenceSize = headerReferenceSize;

        }

        layOut.columnCount =cellCount;

        layOut.minimumLineSpacing =kkPadding *
0.5;

        layOut.minimumInteritemSpacing =kkPadding *0.5;

        layOut.sectionInset =UIEdgeInsetsMake(kkPadding *0.5,
kkPadding *0.5,
kkPadding *0.5,
kkPadding *0.5);

        UICollectionView *collectionView = [[UICollectionViewalloc]
initWithFrame:self.boundscollectionViewLayout:layOut];

        collectionView.tag =self.tag;

        self.collectionView = collectionView;

        collectionView.showsHorizontalScrollIndicator =NO;

        collectionView.showsVerticalScrollIndicator =NO;

        [collectionView registerClass:[SWMainDetailCollectionViewCellclass]
forCellWithReuseIdentifier:mainDetailId];

        if (headClass !=nil) {

            self.headClass = headClass;

            [collectionView registerClass:NSClassFromString(headClass)forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:headScrollId];

        }

        collectionView.backgroundColor =kkLightGrayBg;

        collectionView.dataSource =self;

        collectionView.delegate =self;

        [selfaddSubview:collectionView];

    }

    returnself;

}

#pragma mark - collectionView代理

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

    if (!self.dataLists ||self.dataLists.count
==0) {

        SWVideoListModel *model = [[SWVideoListModelalloc]
init];

        model.goods_id =@"-1";

        self.dataLists = [NSArrayarrayWithObjects:model,
nil];

    }

    returnself.dataLists.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath
*)indexPath {

    SWMainDetailCollectionViewCell *cell = [collectionViewdequeueReusableCellWithReuseIdentifier:mainDetailIdforIndexPath:indexPath];

    cell.tag = indexPath.row;

    cell.delegate =self;

    cell.videoModel =self.dataLists[indexPath.row];

    return cell;

}

#pragma mark - 点击Cell

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath
*)indexPath {

    SWVideoListModel *listModel =self.dataLists[indexPath.row];

    if ([listModel.goods_idisEqualToString:@"-1"])
{

        return;

    }

    [self.delegatevideoCollectionView:selfindex:indexPath.row];

}

#pragma mark - 点击头像

- (void)mainDetailCollectionViewCell:(UICollectionViewCell *)cell {

    SWVideoListModel *listModel =self.dataLists[cell.tag];

    if ([listModel.goods_idisEqualToString:@"-1"])
{

        return;

    }

    [self.delegatevideoCollectionPhotoView:selfindex:cell.tag];

}

#pragma mark - cell的大小用于瀑布流计算值

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout
*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    SWVideoListModel *info =self.dataLists[indexPath.row];

    if ([info.goods_idisEqualToString:@"-1"])
{

        returnCGSizeMake(0,0);

    }

    CGFloat itemWidth = (self.frame.size.width
- (cellCount + 1) *kkPadding *
0.5) /cellCount;

    CGFloat itemH = itemWidth;

    if (info.default_image_height >0
&& info.default_image_width >0) {

        itemH = [info.default_image_heightfloatValue] * itemWidth / [info.default_image_widthfloatValue];

    }

   
//其他文字高度设置

    CGFloat marin =kkPadding *
1.5;

    CGFloat txtHeight = marin;//距离下边距距离

    txtHeight += 35 *0.5;//头像高度

    CGFloat titleHeight = [info._descriptionboundingHeightWithWidth:itemWidth
-kkPadding dict:kkFont13];

    txtHeight += titleHeight;

    itemH += txtHeight;

    returnCGSizeMake(itemWidth, itemH);

}

#pragma mark - 拓展头文件可选

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString
*)kind atIndexPath:(NSIndexPath *)indexPath {

    return [self.delegatecollectionView:collectionViewviewForSupplementaryElementOfKind:kindatIndexPath:indexPath];

}

#pragma mark - 监听滚动的偏移量可选

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    if ([self.delegaterespondsToSelector:@selector(scrollViewDidScroll:scrollView:contentOffset:)])
{

        [self.delegatescrollViewDidScroll:selfscrollView:scrollView
contentOffset:scrollView.contentOffset];

    }

}

#pragma mark - 设置数据源

- (void)setDataLists:(NSArray *)dataLists {

    _dataLists = dataLists;

    [self.collectionViewreloadData];

}

@end

3.自定义SWHeadScrollViewCell、SWMainDetailCollectionViewCell。实现 mainDetailCollectionViewCell代理
。重写layoutSubviews

4.自定义UICollectionViewFlowLayout UICollectionViewFlowLayout.m

#import "SWImgCollectionFlowLayout.h"

@interface
SWImgCollectionFlowLayout ()

@property (nonatomic,strong)
NSMutableDictionary *attributes;

@property (nonatomic,strong)
NSMutableArray *colArr;

@property (nonatomic,assign)
UIEdgeInsets edgeInsets;

@property (nonatomic,weak)
id<UICollectionViewDelegateFlowLayout> delegate;

@property(nonatomic,strong)NSMutableArray
*layoutAttributesArray;

@end

@implementation SWImgCollectionFlowLayout

- (void)prepareLayout {

    // 初始化

    self.attributes = [[NSMutableDictionaryalloc]
init];

    self.layoutAttributesArray = [NSMutableArrayarray];

    self.colArr = [NSMutableArrayarrayWithCapacity:self.columnCount];

    self.delegate = (id<UICollectionViewDelegateFlowLayout>)self.collectionView.delegate;

    // 获取总的个数

    NSUInteger itemCount = [self.collectionViewnumberOfItemsInSection:0];

    if (!itemCount) {

        return;

    }

    //_colArr 用来存放相邻的三个高度

    CGFloat top =.0f;

    for (NSUInteger idx =0; idx <
self.columnCount; idx++) {

        [_colArraddObject:[NSNumbernumberWithFloat:top]];

    }

    //追加头文件布局.

    NSArray *attributesArray1 = [superlayoutAttributesForElementsInRect:CGRectMake(0,0,self.collectionView.frame.size.width,self.collectionView.frame.size.height)];

    for (UICollectionViewLayoutAttributes *headAttributesin attributesArray1)
{

        if (headAttributes.representedElementKind ==UICollectionElementKindSectionHeader)
{

            [self.layoutAttributesArrayaddObject:headAttributes];

        }

    }

    // 遍历所有的item,重新布局

    for (NSUInteger idx =0; idx < itemCount; idx++) {

        [selflayoutItemAtIndexPath:[NSIndexPathindexPathForItem:idx
inSection:0]];

    }

}

//

- (void)layoutItemAtIndexPath:(NSIndexPath *)indexPath {

    // 获取collectionView的edgeInsets

    UIEdgeInsets edgeInsets =self.sectionInset;

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

        edgeInsets = [self.delegatecollectionView:self.collectionViewlayout:selfinsetForSectionAtIndex:indexPath.row];

    }

    self.edgeInsets = edgeInsets;

    

    // 获取collectionView的itemSize

    CGSize itemSize =self.itemSize;

    if ([self.delegaterespondsToSelector:@selector(collectionView:layout:sizeForItemAtIndexPath:)])
{

        itemSize = [self.delegatecollectionView:self.collectionViewlayout:selfsizeForItemAtIndexPath:indexPath];

    }

    

   
// 遍历相邻三个高度获取最小高度

    NSUInteger col =0;

    CGFloat shortHeight = [[_colArrfirstObject]
floatValue];

    for (NSUInteger idx =0; idx <
_colArr.count; idx++) {

        CGFloat height = [_colArr[idx]floatValue];

        if (height < shortHeight) {

            shortHeight = height;

            col = idx;

        }

    }

   
// 得到最小高度的当前Y坐标起始高度

    float top = [[_colArrobjectAtIndex:col]
floatValue];

    

    //追加头文件布局.

    CGFloat headLastY =0;

    NSArray *attributesArray1 = [superlayoutAttributesForElementsInRect:CGRectMake(0,0,self.collectionView.frame.size.width,self.collectionView.frame.size.height)];

    for (UICollectionViewLayoutAttributes *headAttributesin attributesArray1)
{

        if (headAttributes.representedElementKind ==UICollectionElementKindSectionHeader)
{

            headLastY = headAttributes.frame.size.height;

        }

    }

    top = top < headLastY?(top+headLastY):top;

    

    // 设置当前cell的frame

    CGRect frame =CGRectMake(edgeInsets.left + col * (edgeInsets.left
+ itemSize.width), top + edgeInsets.top, itemSize.width, itemSize.height);

    // 把对应的indexPath存放到字典中保存

    [_attributessetObject:indexPath
forKey:NSStringFromCGRect(frame)];

    

    UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributeslayoutAttributesForCellWithIndexPath:indexPath];

    attributes.frame = frame;

    [self.layoutAttributesArrayaddObject:attributes];

    

    // 跟新colArr数组中的高度

    [_colArrreplaceObjectAtIndex:colwithObject:[NSNumbernumberWithFloat:top
+ edgeInsets.top + itemSize.height]];

}

// 系统方法,获取当前可视界面显示的UICollectionViewLayoutAttributes数组

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

    returnself.layoutAttributesArray;

}

// 更新对应UICollectionViewLayoutAttributes的frame

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {

    UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributeslayoutAttributesForCellWithIndexPath:indexPath];

    

    for (NSString *framein
_attributes) {

        if (_attributes[frame] == indexPath) {

            attributes.frame =CGRectFromString(frame);

        }

    }

    

    return attributes;

}

- (CGSize)collectionViewContentSize {

    CGSize size =self.collectionView.frame.size;

    CGFloat maxHeight = [[_colArrfirstObject]
floatValue];

    

   
//查找最高的列的高度

    for (NSUInteger idx =0; idx <
_colArr.count; idx++) {

        CGFloat colHeight = [_colArr[idx]floatValue];

        if (colHeight > maxHeight) {

            maxHeight = colHeight;

        }

    }

    

    size.height = maxHeight +self.edgeInsets.bottom;

    return size;

}
5.使用。

//列表集合

        SWVideoCollectionView *videoView = [[SWVideoCollectionViewalloc]
initWithFrame:self.boundsheaderReferenceSize:CGSizeZeroheadClass:nil];

        videoView.delegate =self;

        self.videoView = videoView;

        videoView.tag =self.tag;

        self.collectionView = videoView.collectionView;

        [self.collectionViewaddHeaderWithTarget:selfaction:@selector(getUp:)];

        [self.collectionViewaddFooterWithTarget:selfaction:@selector(getDown:)];

        [selfaddSubview:videoView];



        //首页集合

        SWVideoCollectionView *mainVideoView = [[SWVideoCollectionViewalloc]
initWithFrame:self.boundsheaderReferenceSize:CGSizeMake(frame.size.width,
frame.size.width *0.65 +
30)headClass:@"SWHeadScrollViewCell"];

        mainVideoView.delegate =self;

        self.mainVideoView = mainVideoView;

        mainVideoView.tag =self.tag;

        self.collectionViewMain = mainVideoView.collectionView;

        [self.collectionViewMainaddHeaderWithTarget:selfaction:@selector(getUp:)];

        [self.collectionViewMainaddFooterWithTarget:selfaction:@selector(getDown:)];

        [selfaddSubview:mainVideoView];

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: