iOS-tableview顶部拉伸效果(头像拉伸)

例如探探个人信息界面拉伸效果,下拉头像放大
这里写图片描述

//
//  PersonController.m
//  Spread
//
//  Created by qiuxuewei on 16/3/21.
//  Copyright © 2016年 邱学伟. All rights reserved.
//

#import "PersonController.h"

@interface PersonController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>{

}

//属性列表
/** 顶部图片视图 */
@property (nonatomic, strong) UIImageView *headerImageView;
@property (nonatomic, strong) UIView *headerBackView;
/** 个人信息界面 */
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation PersonController
#pragma mark - 懒加载
-(UIView *)headerBackView{
    if (_headerBackView == nil) {
        _headerBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 200)];
        [_headerBackView setBackgroundColor:[UIColor lightGrayColor]];
    }
    return _headerBackView;
}
-(UIImageView *)headerImageView{
    if (_headerImageView == nil) {
        _headerImageView = [[UIImageView alloc] init];
        [_headerImageView setImage:[UIImage imageNamed:@"邱_生活.JPG"]];
        [_headerImageView setBackgroundColor:[UIColor greenColor]];
        [_headerImageView setContentMode:UIViewContentModeScaleAspectFill];
        [_headerImageView setClipsToBounds:YES];
    }
    return _headerImageView;
}
-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStyleGrouped];
        [_tableView setDataSource:self];
        [_tableView setDelegate:self];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //添加子视图
    [self addChildViews];
}
#pragma mark - 类内方法
//添加子视图
-(void)addChildViews{
    //添加表格
    [self.view addSubview:self.tableView];
    //添加头像图片
    [self addHeaderImageView];
}
//添加头像
-(void)addHeaderImageView{
    [self.tableView setTableHeaderView:self.headerBackView];
    [self.headerImageView setFrame:self.headerBackView.bounds];
    [self.headerBackView addSubview:self.headerImageView];
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 64;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // 不加此句时,在二级栏目点击返回时,此行会由选中状态慢慢变成非选中状态。
    // 加上此句,返回时直接就是非选中状态。
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

//初始化cell
-(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数据!
    [cell.textLabel setText:@"阿伟"];
    [cell.detailTextLabel setText:@"2016-03-22"];

    return cell;
}

//滚动tableview 完毕之后
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    //图片高度
    CGFloat imageHeight = self.headerBackView.frame.size.height;
    //图片宽度
    CGFloat imageWidth = kScreenWidth;
    //图片上下偏移量
    CGFloat imageOffsetY = scrollView.contentOffset.y;

    NSLog(@"图片上下偏移量 imageOffsetY:%f ->",imageOffsetY);



    //上移
    if (imageOffsetY < 0) {
        CGFloat totalOffset = imageHeight + ABS(imageOffsetY);
        CGFloat f = totalOffset / imageHeight;

        self.headerImageView.frame = CGRectMake(-(imageWidth * f - imageWidth) * 0.5, imageOffsetY, imageWidth * f, totalOffset);
    }

//    //下移
//    if (imageOffsetY > 0) {
//        CGFloat totalOffset = imageHeight - ABS(imageOffsetY);
//        CGFloat f = totalOffset / imageHeight;
//        
//        [self.headerImageView setFrame:CGRectMake(-(imageWidth * f - imageWidth) * 0.5, imageOffsetY, imageWidth * f, totalOffset)];
//    }


}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值