iOS-tableview頂部展開效果(頭像展開)

來源:互聯網
上載者:User

iOS-tableview頂部展開效果(頭像展開)

例如探探個人資訊介面展開效果,下拉頭像放大

////  PersonController.m//  Spread////  Created by qiuxuewei on 16/3/21.//  Copyright ? 2016年 邱學偉. All rights reserved.//#import "PersonController.h"@interface PersonController (){}//屬性列表/** 頂部圖片視圖 */@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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.