美文网首页
下拉放大, 上推渐变

下拉放大, 上推渐变

作者: LST程序员 | 来源:发表于2016-08-20 13:17 被阅读0次
2016-08-20 13_11_15.gif
属性
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)UIView *backGroundView;
@property (nonatomic, strong)UIImageView *imageView;
@property (nonatomic, strong)UIImageView *barImageView;```
######要在导航栏上显示图片

[self.navigationController.navigationBar setBackgroundImage:[UIImage alloc] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];

######创建TableView
  • (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -64, self.view.bounds.size.width, self.view.bounds.size.height + 64)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    _tableView.tableHeaderView = [self creatTableViewHeaderView];
    self.barImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -20, WIDTH, 64)];
    _barImageView.image = [UIImage imageNamed:@"tts-navbar"];
    [self.navigationController.navigationBar addSubview:_barImageView];
    self.navigationItem.title = @"导航栏";
    }
  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 20;
    }
  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.textLabel.text = [NSString stringWithFormat:@"cell = %ld", (long)indexPath.row];
    return cell;
    }
######将图片放在头视图上
  • (UIView *)creatTableViewHeaderView {
    self.backGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
    self.imageView = [[UIImageView alloc] initWithFrame:self.backGroundView.bounds];
    _imageView.image = [UIImage imageNamed:@"1.jpg"];
    [self.backGroundView addSubview:_imageView];
    return _backGroundView;
    }
#####上推渐变, 下拉放大的设置
  • (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.imageView.frame = CGRectMake(-(WIDTH * scale - WIDTH) / 2.0f, -add_height, WIDTH * scale, 200 + add_height);
    NSLog(@"%f", scale);
    }
    }

相关文章

网友评论

      本文标题:下拉放大, 上推渐变

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