UItableview的頭部粘連效果

weixin_33749242發表於2017-03-24

頭部不放大


#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UIImageView *img;
@end

@implementation ViewController


- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    
    [self.view addSubview:self.tableView];

    self.tableView.delegate = self;
    UIImage *im = [UIImage imageNamed:@"hh"];
    UIImageView *img = [[UIImageView alloc] initWithImage:im];
    self.img = img;
    img.frame = CGRectMake(0, -200, self.view.frame.size.width, 200);
    _tableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
    [_tableView addSubview:img];
    
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"ID"];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    

    CGFloat  y = scrollView.contentOffset.y;

    if (scrollView == _tableView) {
        if (y < -200) {
            CGRect frame = self.img.frame;
            frame.size.height =  - y ;
            frame.origin.y = y;
            self.img.frame = frame;
        }
    }
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ID"];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
    return  cell;
    
}

頭部放大



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.hidden = YES;
    
    self.automaticallyAdjustsScrollViewInsets = NO;

    // 設定可伸縮圖片一系列屬性
    UIImage *img = [UIImage imageNamed:@"header"];
    self.header = [[UIImageView alloc] initWithImage:img];
    CGFloat width = self.tableView.frame.size.width;
    CGFloat height = width * 2 / 3;
    self.inOffSet = height;
    
    self.header.frame = CGRectMake(0, -height, width, height);
    [self.tableView addSubview:self.header];
    self.tableView.contentInset = UIEdgeInsetsMake(self.inOffSet, 0, 0, 0);

    
}

// 滾動狀態
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat yoffset = scrollView.contentOffset.y;
    CGFloat xoffset = (yoffset + self.inOffSet)/ 2;
    
    if (yoffset < -self.inOffSet) {
        CGFloat width = scrollView.frame.size.width;
        self.header.frame = CGRectMake(xoffset, yoffset, width - xoffset * 2, -yoffset);
    }
    
}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    // Return the number of rows in the section.
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 測試資料
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    cell.textLabel.text = [NSString stringWithFormat:@" %ld", indexPath.row];
    
    return cell;
}


相關文章