美文网首页
TableView 头部下拉放大

TableView 头部下拉放大

作者: 1剑天下 | 来源:发表于2016-12-24 18:02 被阅读113次

功能:下拉tableView 头部放大 ,上拉头部渐隐,显示导航栏

效果图:

  1. 显示导航栏


    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];
    
    // 取消自动布局间距 (否则 第一个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];
}
/**
 *  创建maintable
 */
#pragma mark-
#pragma mark- 创建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 ;
    
    // 设置tableview的内边距 这里是让tableview距离顶部空出来200pt 用来放置头部视图
    
     _mainTable.contentInset = UIEdgeInsetsMake(HEADER_H, 0, 0, 0);
    
    // 同样 设置右侧指示器的内间距 距离顶部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 代理方法实现
 */

-(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未拉动,>0向上 <0向下
    CGFloat offset = scrollView.contentOffset.y + scrollView.contentInset.top;
    
    

    if (offset<=0) {
    //   向下拉动
        
        //改变头部视图的高度
        
        _HeaderView.y=0; // 必须的加上 , 例如 :如果这个不加的话 contsize 由-20 直接到20,_HeaderView的y无法固定到顶部
        
        _HeaderView.height =  HEADER_H-offset;
       
        
        _headeimageview.height = HEADER_H-offset;
        
        _headeimageview.alpha =1;//将图片完全显示出来
        
        _nav.hidden =YES;//隐藏导航栏
        
    }
    else
    {
         // 向上拉动
        
        _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 设置头部视图的y为-HEADER_H+64 预留一个64的导航栏
            _HeaderView.y =-HEADER_H+64;
            
           _headeimageview.alpha= 0;
            _nav.hidden =NO;
            
        }
     
    }
   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end```

相关文章

网友评论

      本文标题:TableView 头部下拉放大

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