功能:下拉tableView 頭部放大 ,上拉頭部漸隱,顯示導航欄
效果圖:
-
顯示導航欄
B0C9B786-B10D-4109-B5B9-365E87A25374.png -
漸隱
- 放大
代碼部分
//
// 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```