NavigationBar上拉漸變, 下拉圖片放大效果

首先, 簡述一下我想實現(xiàn)的效果, 整個頁面是以一個tableView為主的簡單瀏覽頁, tableView.headView上有一張圖片.
效果1: 當(dāng)下拉頁面時, 圖片放大, 當(dāng)松開手指時, 圖片恢復(fù)到原來大小
效果2: 當(dāng)上拉頁面, 從視圖逐漸消失時, NavigationBar出現(xiàn)并伴隨漸變效果

NavgationBar動畫.gif

廢話不多說, 先上代碼(我將按照我自己的思路逐步講解):

  1. 將屏幕寬度和屏幕高度設(shè)為宏,簽協(xié)議, 并根據(jù)需求設(shè)置屬性
#define kScreenWith [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
@interface ViewController ()
<
    UITableViewDataSource,
    UITableViewDelegate,
    UIScrollViewDelegate
>
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) UIView *backViewForTableHeaderView;
@property (nonatomic, strong) UIImageView *backImageViewForTableHeaderView;
@property (nonatomic, strong) UIImageView *barImageView;
@property (nonatomic, strong) UILabel *nameLabel;
@end
- (void)viewDidLoad {
   [super viewDidLoad];
   // 創(chuàng)建tableView
   [self createTableView];   
   // 處理navigationBar
   [self handleNavigationBar];
}
  1. 創(chuàng)建一個tableView(這一步就不用我多說了吧), 這里創(chuàng)建tableView時, 要將y坐標(biāo)設(shè)為-64, 讓tableView顯示在NavgationBar的下方, 同時tableView的高度要+64, 以保證占據(jù)整個屏幕
- (void)createTableView {
   self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -64, kScreenWidth,kScreenHeight + 64)];
   _myTableView.backgroundColor = [UIColor whiteColor];
   _myTableView.delegate = self;
   _myTableView.dataSource = self;
   [_myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
   [self.view addSubview:_myTableView];
   _myTableView.tableHeaderView = [self createTableHeaderView];
}

創(chuàng)建tableView頭視圖, 頭視圖上放置一個imageView和一個label.

 - (UIView *)createTableHeaderView {
    self.backViewForTableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 200)];
    self.backImageViewForTableHeaderView = [[UIImageView alloc] initWithFrame:self.backViewForTableHeaderView.bounds];
    _backImageViewForTableHeaderView.image = [UIImage imageNamed:@"1.jpg"];
    [self.backViewForTableHeaderView addSubview:_backImageViewForTableHeaderView];
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 150, 200, 40)];
    _nameLabel.text = @"這游戲真難";
    [self.backViewForTableHeaderView addSubview:_nameLabel];
    return _backViewForTableHeaderView;
}
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 50;
}
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    cell.textLabel.text = [NSString stringWithFormat:@"cell- %ld", indexPath.row];
    return cell;
}
  1. 在navigationBar上創(chuàng)建一個imageView用來顯示背景圖片,
 - (void)handleNavigationBar {
    // 去掉背景圖片
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    // 去掉navigationBar底部線條
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    // 此處y設(shè)置為-20, 否則狀態(tài)欄不能被覆蓋
    self.barImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -20, kScreenWidth, 64)];
    _barImageView.image = [UIImage imageNamed:@"2.png"];
    [self.navigationController.navigationBar addSubview:_barImageView];
    self.navigationItem.title = @"導(dǎo)航欄";
}
  1. 在tableView滑動方法中更改navigationBar上背景圖片的透明度, 和tableHeaderView上的imageView的frame, 故要將tableView的偏移量與bar上imageView的透明度關(guān)聯(lián), 并將tableHeaderView上的frame與偏移量關(guān)聯(lián), 實現(xiàn)上拉導(dǎo)航欄漸變, 下拉圖片放大的效果.
 - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offset_Y = scrollView.contentOffset.y;
    CGFloat alpha = (offset_Y + 64) / 200.0f;
    self.barImageView.alpha = alpha;
    if (offset_Y < -64) {
        CGFloat add_height = -(offset_Y + 64);
        CGFloat scale = (200 + add_height) / 200.0f;
        self.backImageViewForTableHeaderView.frame = CGRectMake(-(kScreenWidth * scale - kScreenWidth) / 2.0f, -add_height, kScreenWidth * scale, 200 + add_height);
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • *7月8日上午 N:Block :跟一個函數(shù)塊差不多,會對里面所有的內(nèi)容的引用計數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,737評論 1 14
  • 前言 最近忙完項目比較閑,想寫一篇博客來分享一些自學(xué)iOS的心得體會,希望對迷茫的你有所幫助。博主非科班出身,一些...
    GitHubPorter閱讀 1,588評論 9 5
  • 1.badgeVaule氣泡提示 2.git終端命令方法> pwd查看全部 >cd>ls >之后桌面找到文件夾內(nèi)容...
    i得深刻方得S閱讀 4,981評論 1 9
  • 做一件有難度的事情,之前又從來沒有過成功的經(jīng)驗,該如何確保自己能夠一路堅持下去呢。 在做事情之前,很多時候都會被自...
    山頂?shù)暮诠沸?/span>閱讀 198評論 0 0
  • 九九是我從格爾木返回西寧的火車上認(rèn)識的一個女孩,18歲,剛高中畢業(yè),瘦小的身影,怎么看都覺得應(yīng)該有個人陪伴才叫人放...
    莫思辰閱讀 1,402評論 0 1

友情鏈接更多精彩內(nèi)容