滾動放大圖片 2018-09-21

屏幕快照 2018-09-21 下午5.07.19.png

屏幕快照 2018-09-21 下午5.08.16.png
  1. 進(jìn)入頁面隱藏導(dǎo)航欄 退出頁面顯示導(dǎo)航欄

- (void)viewWillAppear:(BOOL)animated {

    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {

    self.navigationController.navigationBar.hidden = NO;
}

2.依次創(chuàng)建tableView, headImg, navView
tableView要設(shè)置contentInset,top值為圖片高度-20

static const CGFloat kHeadImgWidth = 375;
static const CGFloat kHeadImgHeight = 165;


- (UITableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
        CGFloat headImvH = SCREEN_WIDTH / kHeadImgWidth * kHeadImgHeight;
        _tableView.contentInset = UIEdgeInsetsMake(headImvH-20, 0, 0, 0);
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

- (UIImageView *)headImv {
    
    if (!_headImg) {
        _headImg = [[UIImageView alloc] init];
        CGFloat headImvH = SCREEN_WIDTH / kHeadImgWidth * kHeadImgHeight;
        _headImg.frame = CGRectMake(0, 0, SCREEN_WIDTH, headImvH);
        _headImg.image = [UIImage imageNamed:@"login_bg"];
        _headImg.contentMode = UIViewContentModeScaleAspectFill;
        _headImg.clipsToBounds = YES;
    }
    return _headImg;
}

- (UIView *)navView {
    
    if (!_navView) {
        _navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
        _navView.backgroundColor = CellWordDarkColor;
        _navView.clipsToBounds = YES;
        _titLab = [[UILabel alloc]init];
        _titLab.centerX = SCREEN_WIDTH * 0.5;
        _titLab.bounds = CGRectMake(0, 0, 150, 44);
        _titLab.textAlignment = NSTextAlignmentCenter;
        _titLab.font = [UIFont systemFontOfSize:12];
        _titLab.textColor = [UIColor whiteColor];
        _titLab.numberOfLines = 0;
        _titLab.text = @"明天休息了";
        [_navView addSubview:_titLab];
    }
    return _navView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = GlobalBGColor;
    
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.headImv];
    [self.view addSubview:self.navView];
}

  1. 滾動時調(diào)用scrollView的代理方法(重要)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat offsety = scrollView.contentOffset.y;
    //  按比例換算出頭像的初始化高度
    NSInteger headerH = SCREEN_WIDTH / kHeadImgWidth * kHeadImgHeight;
    //  拉伸后的高度 = 實際高度+偏移量
    CGFloat offset = headerH + offsety;
    
    //  圖片移出可視范圍,導(dǎo)航欄逐漸顯示;圖片進(jìn)入視線,逐漸隱藏
    //  標(biāo)題的高度亦隨之變化
    self.navView.alpha = offset / 180;
    
    if (self.navView.alpha >= 1) {
        self.navView.alpha = 1;
    }
    
    self.titLab.Y = 64 - 44 * self.navView.alpha;
    
    //  圖片的高度隨其他內(nèi)容顯隱而隨之變化
    self.headImv.frame = CGRectMake(0, 0, SCREEN_WIDTH, headerH - offset);
}
  1. tableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"測試數(shù)據(jù)——%td",indexPath.row];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"測試數(shù)據(jù)——%td",indexPath.row];
    return cell;
}
最后編輯于
?著作權(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)容

  • Qt編寫模塊化插件式應(yīng)用程序 優(yōu)點: 1. 責(zé)任分塊,多人協(xié)同開發(fā)應(yīng)用程序,每個人只需要關(guān)心自己模塊內(nèi)部實現(xiàn)即可,...
    豪爵吸金ing閱讀 4,133評論 0 2
  • 上學(xué)時,有位舍友的書架上放了一本《豐田生產(chǎn)管理方式》,厚厚的一本書,虎黃色的書封。我拿來翻了幾頁,只記住了一個詞:...
    梅子的日志閱讀 863評論 0 4
  • 其實這個文章標(biāo)題并不太契合實際,最近工作都不太忙,所以也就不存在忙里偷閑了。但是,突然從腦中蹦出來,于是,姑且一用...
    清幽客閱讀 224評論 0 2
  • “主持,弟子依舊放不下凡塵俗世,望主持準(zhǔn)許弟子還俗?!?他聲音滿是滄桑,卻擲地有聲,仿若經(jīng)過了激烈的思考。 “悟塵...
    斟茶公子閱讀 943評論 10 8

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