TableView 頭部下拉放大

功能:下拉tableView 頭部放大 ,上拉頭部漸隱,顯示導(dǎo)航欄

效果圖:

  1. 顯示導(dǎo)航欄


    B0C9B786-B10D-4109-B5B9-365E87A25374.png
  2. 漸隱


  1. 放大

代碼部分

//
//  XW_TableViewVC.m
//  頭部下拉刷新
//
//  Created by 文 on 2016/12/24.
//  Copyright ? 2016年 文. All rights reserved.
//

#import "XW_TableViewVC.h"
#import "UIView+Extension.h"
static NSString *XW_TableViewCellID = @"XW_TableViewCellID";

#define HEADER_H 200

@interface XW_TableViewVC ()<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray *_dataArr;
    
    UIView *_HeaderView;
    
    UIImageView *_headeimageview;
    
    UIView *_nav;
   
}

@property(strong, nonatomic) UITableView * mainTable;;
@end

@implementation XW_TableViewVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _dataArr = [[NSMutableArray alloc] init];
    
    [self.navigationController setNavigationBarHidden:YES];
    
    // 取消自動(dòng)布局間距 (否則 第一個(gè)cell與頂部有部分間距)
    
    self.automaticallyAdjustsScrollViewInsets =NO;
    
    [self CreatHeaderView];

    
    [self CreatMainTable];
   
}




-(void)CreatHeaderView
{
    
    _HeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, HEADER_H)];
    
    
    
    
    _HeaderView.backgroundColor =[UIColor redColor];
    
    

    
   _headeimageview = [[UIImageView alloc] initWithFrame:_HeaderView.bounds];
    
    
    _headeimageview.image =[UIImage imageNamed:@"icon-120.png"];
    

    _headeimageview.backgroundColor =[UIColor redColor];
    
    
    [_HeaderView addSubview:_headeimageview];
    
    //等比例填充不然變形
    _headeimageview.contentMode  = UIViewContentModeScaleAspectFill;
    
    _headeimageview.layer.masksToBounds = YES;
    
    _nav =[[UIView alloc] initWithFrame:CGRectMake(0, _HeaderView.height-64, _HeaderView.width, 64)];
    
    UIButton * leftbut =[UIButton buttonWithType:UIButtonTypeCustom];

    leftbut.frame =CGRectMake(15, 25, 30, 30);
    
    [leftbut addTarget:self action:@selector(ClickLeftBut) forControlEvents:UIControlEventTouchUpInside];
    
    leftbut.backgroundColor = [UIColor yellowColor];
    
    [_nav addSubview:leftbut];
    
    [_HeaderView addSubview:_nav];
}

-(void)ClickLeftBut
{
    [self.navigationController popViewControllerAnimated:YES];
}
/**
 *  創(chuàng)建maintable
 */
#pragma mark-
#pragma mark- 創(chuàng)建tableview

-(void)CreatMainTable
{
    _mainTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
    
    _mainTable.delegate = self ;
    
    _mainTable.dataSource = self ;
    
    _mainTable.separatorStyle = UITableViewCellSeparatorStyleNone ;
    
    // 設(shè)置tableview的內(nèi)邊距 這里是讓tableview距離頂部空出來200pt 用來放置頭部視圖
    
     _mainTable.contentInset = UIEdgeInsetsMake(HEADER_H, 0, 0, 0);
    
    // 同樣 設(shè)置右側(cè)指示器的內(nèi)間距 距離頂部200
    _mainTable.scrollIndicatorInsets =UIEdgeInsetsMake(HEADER_H, 0, 0, 0);
    
    


     [_mainTable registerClass:[UITableViewCell class] forCellReuseIdentifier:XW_TableViewCellID];
    
    [self.view addSubview:self.mainTable];
    
    [self.view addSubview:_HeaderView];
    
    
}


/**
 *  mainTable 代理方法實(shí)現(xiàn)
 */

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:XW_TableViewCellID];
    

    cell.textLabel.text=[NSString stringWithFormat:@"%ld",indexPath.row];
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50.0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
   // NSLog(@"%lf",scrollView.contentOffset.y);
    
    // 由于contoffset 起始位置向下偏移了200 ,所有加上歧視偏移的高度 , = 0未拉動(dòng),>0向上 <0向下
    CGFloat offset = scrollView.contentOffset.y + scrollView.contentInset.top;
    
    

    if (offset<=0) {
    //   向下拉動(dòng)
        
        //改變頭部視圖的高度
        
        _HeaderView.y=0; // 必須的加上 , 例如 :如果這個(gè)不加的話 contsize 由-20 直接到20,_HeaderView的y無法固定到頂部
        
        _HeaderView.height =  HEADER_H-offset;
       
        
        _headeimageview.height = HEADER_H-offset;
        
        _headeimageview.alpha =1;//將圖片完全顯示出來
        
        _nav.hidden =YES;//隱藏導(dǎo)航欄
        
    }
    else
    {
         // 向上拉動(dòng)
        
        _HeaderView.height =  HEADER_H; // 必須的加上
        
        
        _headeimageview.height = HEADER_H; //必須的加上

       
        
        if (HEADER_H- 64>=offset) {
            
            
            _HeaderView.y =-offset;
            
            
            _headeimageview.y =0;
            
            _headeimageview.alpha =1-offset/(HEADER_H-64.0);
            
           _nav.hidden =YES;
        }
        else
        {
                // 上拉高度超過了HEADER_H- 64 設(shè)置頭部視圖的y為-HEADER_H+64 預(yù)留一個(gè)64的導(dǎo)航欄
            _HeaderView.y =-HEADER_H+64;
            
           _headeimageview.alpha= 0;
            _nav.hidden =NO;
            
        }
     
    }
   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end```
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容