异常风骚的吸顶动画

197 阅读1分钟
#import "ViewController.h"
#import <Masonry/Masonry.h>

static CGFloat navHeight = 60;

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UIView *navView;
@property (nonatomic,strong) UIImageView *iconView;
@property (nonatomic,strong) UIImageView *animationIconView;
@property (nonatomic,assign) CGFloat iconBeginY;

@property (nonatomic,strong) UIButton *followBtn;
@property (nonatomic,strong) UIButton *animationFollowBtn;
@property (nonatomic,assign) CGFloat followBeginY;
@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,assign) CGFloat iconMinY;
@property (nonatomic,assign) CGFloat followMinY;

@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setupSubviews];
    
    [self.tableView reloadData];
    [self.view layoutIfNeeded];
    [self.tableView layoutIfNeeded];

    [self addAnimationIconView];
    [self addAnimationFollowBtn];
}

- (void)setupSubviews {
    self.view.backgroundColor = [UIColor colorWithRed:127/255.f green:227/255.f blue:240/255.f alpha:1];
    
    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, navHeight + [UIApplication sharedApplication].statusBarFrame.size.height)];
    self.navView = navView;
    navView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:navView];
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView = tableView;
    [self.view addSubview:tableView];
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(navView.mas_bottom);
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 150;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectZero];
    headerView.backgroundColor = [UIColor colorWithRed:127/255.f green:227/255.f blue:240/255.f alpha:1];
    
    UIImageView *iconView = [[UIImageView alloc] init];
    self.iconView = iconView;
    iconView.layer.cornerRadius = 25.f;
    iconView.layer.masksToBounds = YES;
    iconView.image = [UIImage imageNamed:@"header.png"];
    [headerView addSubview:iconView];
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(20);
        make.width.height.mas_equalTo(50);
    }];
    
    UILabel *textLabel = [[UILabel alloc] init];
    textLabel.backgroundColor = [UIColor clearColor];
    textLabel.textAlignment = NSTextAlignmentCenter;
    textLabel.textColor = UIColor.whiteColor;
    textLabel.font = [UIFont systemFontOfSize:18.f];
    textLabel.text = @"FaceWaller";
    [headerView addSubview:textLabel];
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(iconView.mas_right).mas_offset(10);
        make.centerY.mas_equalTo(iconView.mas_centerY);
    }];
    
    UILabel *followLabel = [[UILabel alloc] init];
    followLabel.backgroundColor = [UIColor clearColor];
    followLabel.textColor = UIColor.whiteColor;
    followLabel.text = @"关注数:100";
    followLabel.font = [UIFont systemFontOfSize:12.f];
    [headerView addSubview:followLabel];
    [followLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(iconView);
        make.top.mas_equalTo(iconView.mas_bottom).mas_offset(10);
    }];
    
    UILabel *infoLabel = [[UILabel alloc] init];
    infoLabel.backgroundColor = [UIColor clearColor];
    infoLabel.textColor = UIColor.whiteColor;
    infoLabel.text = @"一句话介绍自己!";
    infoLabel.font = [UIFont systemFontOfSize:14.f];
    [headerView addSubview:infoLabel];
    [infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(followLabel);
        make.top.mas_equalTo(followLabel.mas_bottom).mas_offset(10);
    }];
    
    UIButton * followBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 80, 80, 60, 20)];
    [followBtn setTitle:@"关注" forState:UIControlStateNormal];
    followBtn.backgroundColor = [UIColor colorWithRed:240/255.f green:240/255.f blue:247/255.f alpha:1];
    [followBtn setTitleColor:[UIColor colorWithRed:33/255.f green:196/255.f blue:255/255.f alpha:1] forState:UIControlStateNormal];
    followBtn.titleLabel.font = [UIFont systemFontOfSize:14.f];
    followBtn.layer.cornerRadius = 10.f;
    [followBtn.layer masksToBounds];
    [headerView addSubview:followBtn];
    self.followBtn = followBtn;
    
    return headerView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 30;
}

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

- (void)addAnimationIconView {
    if (self.animationIconView) return;
    UIImageView *animationIconView = [[UIImageView alloc]init];
    self.animationIconView = animationIconView;
    animationIconView.layer.cornerRadius = self.iconView.layer.cornerRadius;
    animationIconView.layer.masksToBounds = self.iconView.layer.masksToBounds;
    animationIconView.image = self.iconView.image;
    [self.view addSubview:animationIconView];
    CGRect newFrame = [self.view convertRect:self.iconView.frame fromView:self.tableView];
    animationIconView.frame = newFrame;
    self.iconBeginY = newFrame.origin.y;
    self.iconMinY = (navHeight - newFrame.size.height)/2 + [UIApplication sharedApplication].statusBarFrame.size.height;
}

- (void)addAnimationFollowBtn {
    if (self.animationFollowBtn) return;
    UIButton *animationFollowBtn = [[UIButton alloc]init];
    self.animationFollowBtn = animationFollowBtn;
    animationFollowBtn.layer.cornerRadius = self.followBtn.layer.cornerRadius;
    [animationFollowBtn setTitleColor:self.followBtn.currentTitleColor forState:UIControlStateNormal];
    animationFollowBtn.backgroundColor = self.followBtn.backgroundColor;
    [animationFollowBtn setTitle:self.followBtn.titleLabel.text forState:UIControlStateNormal];
    animationFollowBtn.titleLabel.font = self.followBtn.titleLabel.font;
    [self.view addSubview:animationFollowBtn];
    CGRect newFrame = [self.view convertRect:self.followBtn.frame fromView:self.tableView];
    animationFollowBtn.frame = newFrame;
    self.followBeginY = newFrame.origin.y;
    self.followMinY = (navHeight - newFrame.size.height)/2 + [UIApplication sharedApplication].statusBarFrame.size.height;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGRect iconFrame = self.animationIconView.frame;
    CGFloat newIconY = self.iconBeginY - scrollView.contentOffset.y;
    if (newIconY < self.iconMinY) {
        newIconY = self.iconMinY;
    }
    iconFrame.origin.y = newIconY;
    self.animationIconView.frame = iconFrame;
    
    CGRect followBtnFrame = self.animationFollowBtn.frame;
    CGFloat newFollowBtnY = self.followBeginY - scrollView.contentOffset.y;
    if (newFollowBtnY < self.followMinY) {
        newFollowBtnY = self.followMinY;
    }
    followBtnFrame.origin.y = newFollowBtnY;
    self.animationFollowBtn.frame = followBtnFrame;
}

@end