美文网首页objective-c
滚动放大图片 2018-09-21

滚动放大图片 2018-09-21

作者: 戊戌水瓶 | 来源:发表于2018-09-21 16:54 被阅读0次
屏幕快照 2018-09-21 下午5.07.19.png
屏幕快照 2018-09-21 下午5.08.16.png
  1. 进入页面隐藏导航栏 退出页面显示导航栏

- (void)viewWillAppear:(BOOL)animated {

    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {

    self.navigationController.navigationBar.hidden = NO;
}

2.依次创建tableView, headImg, navView
tableView要设置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. 滚动时调用scrollView的代理方法(重要)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat offsety = scrollView.contentOffset.y;
    //  按比例换算出头像的初始化高度
    NSInteger headerH = SCREEN_WIDTH / kHeadImgWidth * kHeadImgHeight;
    //  拉伸后的高度 = 实际高度+偏移量
    CGFloat offset = headerH + offsety;
    
    //  图片移出可视范围,导航栏逐渐显示;图片进入视线,逐渐隐藏
    //  标题的高度亦随之变化
    self.navView.alpha = offset / 180;
    
    if (self.navView.alpha >= 1) {
        self.navView.alpha = 1;
    }
    
    self.titLab.Y = 64 - 44 * self.navView.alpha;
    
    //  图片的高度随其他内容显隐而随之变化
    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:@"测试数据——%td",indexPath.row];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"测试数据——%td",indexPath.row];
    return cell;
}

相关文章

网友评论

    本文标题:滚动放大图片 2018-09-21

    本文链接:https://www.haomeiwen.com/subject/gfcznftx.html