滚动视图,某个视图到顶部停留

主要代码

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);

    }

}


(2)UICollectionView

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







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值