超简单的UITableView下拉放大的动画

36 篇文章 0 订阅
29 篇文章 0 订阅

很多主流的app都会有这个动画,然后用最简单的思想实现了一下,就是按照tableView的contentOffset,直接进行缩放,效果还不错.

这里写图片描述

#import "ViewController.h"
#import <Masonry/Masonry.h>

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UITableViewCell *headerCell;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //让automaticallyAdjustsScrollViewInsets为NO,是为了当有导航栏的时候,防止系统自动设置tableView的contentInset,这里并没有加navigationController
    //self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.tableView];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.frame];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    }
    return _tableView;
}

- (UIImageView *)imageView
{
    if (!_imageView) {
        _imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"user_bg"]];
        _imageView.clipsToBounds = YES;

    }
    return _imageView;
}

- (UITableViewCell *)headerCell
{
    if (!_headerCell) {
        _headerCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"headerCell"];

        //创建第一个view当做背景,令其clipsToBounds属性为yes,让imageView超出view的部分切掉
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor whiteColor];
        view.clipsToBounds = YES;

        //给view加上左,右,下三个方向上的约束
        [_headerCell.contentView addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_headerCell.contentView);
            make.right.equalTo(_headerCell.contentView);
            make.bottom.equalTo(_headerCell.contentView);
        }];
        [view addSubview:self.imageView];
        [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(view);
        }];
    }
    return _headerCell;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return self.headerCell;
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.textLabel.text = [NSString stringWithFormat:@"zhhh%ld",indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 200;
    }
    return 44;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //利用contentOffset.y进行动画
    if (scrollView.contentOffset.y < 0) {
        self.imageView.transform = CGAffineTransformMakeScale(1 - (scrollView.contentOffset.y / self.view.frame.size.width), 1 - (scrollView.contentOffset.y / self.view.frame.size.width));
    }
}
@end
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值