主要代码
1、控制器中
-(void)creatHomeView{
homeView *home = [[homeViewalloc] initWithFrame:CGRectMake(0,0, 375,667-64)];
[self.viewaddSubview:home];
__weakSecondViewController *weakself = self;
home.setBlock = ^(CGFloat offSet){
if (offSet >200) {
weakself.headerView.center =CGPointMake(weakself.headerView.center.x,_yOffset - 200);
return;
}
CGFloat h =_yOffset - offSet;
weakself.headerView.center =CGPointMake(weakself.headerView.center.x, h);
};
}
-(void)creatHeader{
_headerView = [[UIViewalloc] initWithFrame:CGRectMake(0,0, 375,260)];
_headerView.backgroundColor = [UIColoryellowColor];
[self.viewaddSubview:_headerView];
_yOffset =_headerView.center.y;
UIView *sectionView = [[UIViewalloc] initWithFrame:CGRectMake(0,200, 375,60)];
sectionView.backgroundColor = [UIColorcyanColor];
[_headerViewaddSubview:sectionView];
}
2、自定义视图homeView中
(1)scrollView
-(instancetype)initWithFrame:(CGRect)frame{
self = [superinitWithFrame:frame];
if (self) {
UIScrollView *scroll = [[UIScrollViewalloc] initWithFrame:CGRectMake(0,0, frame.size.width, frame.size.height)];
scroll.backgroundColor = [UIColorpurpleColor];
scroll.delegate =self;
scroll.contentSize =CGSizeMake(375,1000);
[selfaddSubview:scroll];
}
returnself;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSInteger offSet = scrollView.contentOffset.y;
if (self.setBlock) {
self.setBlock(offSet);
}
}
homeView.h
#import <UIKit/UIKit.h>
typedef void(^OffSetBlock)(CGFloat offSet);
@interface homeView : UIView<UICollectionViewDataSource ,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong) UICollectionView *collectionView;
@property(nonatomic,copy)OffSetBlock setBlock;
@end
homeView.m
#import "homeView.h"
#define _CELL @"CELL"
@interface homeView()<UIScrollViewDelegate>
@end
@implementation homeView
- (instancetype)initWithFrame:(CGRect)frame{
self = [superinitWithFrame:frame];
if (self) {
//
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
// 创建一屏的视图大小
UICollectionView *collectionView = [[UICollectionViewalloc] initWithFrame:CGRectZerocollectionViewLayout:flowLayout];
[collectionView registerClass:[UICollectionViewCellclass] forCellWithReuseIdentifier:_CELL];
collectionView.delegate =self ;
collectionView.dataSource =self ;
collectionView.backgroundColor = [UIColorbrownColor ];
self.collectionView = collectionView;
self.collectionView.frame =CGRectMake(0,0, 375, frame.size.height);
self.collectionView.contentInset =UIEdgeInsetsMake(260,0, 0,0);
[self addSubview:collectionView];
}
return self;
}
#pragma mark - UICollectionViewDataSource
//定义展示的UICollectionViewCell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 31;
}
// 定义展示的Section的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1 ;
}
// 每个UICollectionView展示的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell * cell = [collectionViewdequeueReusableCellWithReuseIdentifier:_CELL forIndexPath :indexPath];
cell. backgroundColor = [UIColorgreenColor];
return cell;
}
#pragma mark - UICollectionViewDelegate
// UICollectionView被选中时调用的方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell* cell = (UICollectionViewCell *)[collectionViewcellForItemAtIndexPath:indexPath];
cell. backgroundColor = [UIColorredColor];
}
// 返回这个UICollectionViewCell是否可以被选择
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
#pragma mark - UICollectionViewDelegateFlowLayout
// 定义每个UICollectionView的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat item_w = (collectionView.bounds.size.width-30)/2;
CGFloat item_h = 115+item_w;
CGSize size = CGSizeMake(item_w, item_h);
return size;
}
// 定义每个UICollectionView的边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake (10 ,10 ,10 ,10);
}
#pragma mark -
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat offSet = scrollView.contentOffset.y+260;
if (self.setBlock) {
self.setBlock(offSet);
}
}
@end