一種典型的個人中心效果實現

版本記錄

版本號 時間
V1.0 2018.01.15

前言

很多的APP個人中心都有自己的特點,但是其中有一種很經典的形式,就是頂部的封面圖是可以隨著向下拖動視圖出現拉伸放大外擴的效果,松開手就會回到最初的那種形式,這一篇我們就看一下這種效果的簡單實現。

功能需求

實現類似QQ空間和其他APP經典個人中心的向下拉伸外擴的效果。

這兩款APP的效果就是向下拉,頂部的封面會有外擴的效果,松開手就會彈回去。


功能實現

下面我們就看一下實現的代碼。

1. ViewController.m

#import "ViewController.h"

#define kViewControllerCellReuseIdentify   @"kViewControllerCellReuseIdentify"

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>

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

@end

@implementation ViewController

#pragma mark -  Override Base Function

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initUI];
}

#pragma mark -  Object Private Function

- (void)initUI
{
    //tableview
    CGRect frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width,self.view.bounds.size.height);
    self.tableView = [[UITableView alloc] initWithFrame: frame style:UITableViewStylePlain];
    self.tableView.rowHeight = 60.0;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kViewControllerCellReuseIdentify];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    
    //頂部視圖
    self.headerTopView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 250.0 + 80.0)];
    self.headerTopView.image = [UIImage imageNamed:@"scene"];
    self.headerTopView.contentMode = UIViewContentModeScaleAspectFill;
    self.headerTopView.backgroundColor = [UIColor blueColor];
    [self.view insertSubview: self.headerTopView belowSubview: self.tableView];
    
    CGRect headerFrame = CGRectMake(0, 0, self.view.bounds.size.width, 250.0);
    UIView *realHeaderView = [[UIView alloc] initWithFrame: headerFrame];
    realHeaderView.backgroundColor = [UIColor clearColor];

    self.tableView.tableHeaderView = realHeaderView;
}

- (void)handleHeaderView: (CGFloat)offsetY maxOffset: (CGFloat)max
{
    if (offsetY < 0) {
        self.headerTopView.frame = CGRectMake(0, 0, self.headerTopView.bounds.size.width, self.headerTopView.intrinsicContentSize.height - offsetY);
    }
    
    if (offsetY > 0) {
        CGRect frame = self.headerTopView.frame;
        frame.origin.y = -offsetY;
        self.headerTopView.frame = frame;
    }
    
    if (offsetY == 0) {
        self.headerTopView.frame = CGRectMake(0, - offsetY, self.headerTopView.bounds.size.width, self.headerTopView.intrinsicContentSize.height);
    }
}

#pragma mark -  UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView != self.tableView){
        return;
    }
    
    CGFloat offsetY = scrollView.contentOffset.y;
    CGFloat tabOffsetY = [self.tableView rectForSection: 0].origin.y;
    [self handleHeaderView: offsetY maxOffset: tabOffsetY];
}

#pragma mark -  UITableViewDelegate, UITableViewDataSource

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kViewControllerCellReuseIdentify forIndexPath:indexPath];
    CGFloat redValue = arc4random_uniform(255)/255.0;
    CGFloat greenValue = arc4random_uniform(255)/255.0;
    CGFloat blueValue = arc4random_uniform(255)/255.0;
    cell.backgroundColor = [UIColor colorWithRed:redValue green:greenValue blue:blueValue alpha:1.0];
    return cell;
}

@end

這個實現還是很簡單的,這里采用的視圖層次是,

這里:

    1. 就是一個UIImageView控件,用于放封面。
    1. 是一個底色透明的UITableView
    1. 是一個tableHeaderView,底色也是透明的,高度和封面UIImageView是一樣的。

上面就是設計的視圖層次。


功能效果

下面我們就看一下功能效果。

后記

未完,待續~~~

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 雨夜隨行云慘淡 撕心裂肺喚魂還 片片泥骨不應語 三月時歸撲腳前 親人莫怪兒姍姍 嘮嘮叨叨又星點 兒時記憶歷歷目 一...
    栩辰徉閱讀 542評論 4 10
  • 倒立撐是一個有名又有效的動作,相信有人還是認識的,而且倒立撐是有一些好處的,同時倒立撐也是有不少講究的,那倒立撐怎...
    sjcccc閱讀 486評論 0 0
  • 昨天與妹妹走路5公里,晚上9點一過就困了,人啊,還得動! 與妹妹聊天特舒服,喜歡她說話,特別逗!陽光下,姐妹倆,聊...
    影子3623253閱讀 270評論 3 4