头部视觉差效果

95 阅读1分钟
#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *baseView;
@property (nonatomic,strong) UIImageView *iconImageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationController setNavigationBarHidden:YES animated:NO];![](https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/71854adc81c143ae94d8dba42a89ea2f~tplv-k3u1fbpfcp-watermark.image)
    [self.view addSubview:self.tableView];
    self.tableView.tableHeaderView = self.baseView;
}

- (UIView *)baseView {
    if (!_baseView) {
        _baseView = [[UIView alloc] init];
        _baseView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.1];
        _baseView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200);
        _iconImageView = [[UIImageView alloc] init];
        _iconImageView.image = [UIImage imageNamed:@"zrx4.jpg"];
        _iconImageView.frame = _baseView.bounds;
        [_baseView addSubview:_iconImageView];
    }
    return _baseView;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 0;
        _tableView.sectionHeaderHeight = 0.0;
        _tableView.sectionFooterHeight = 0.0;
        _tableView.estimatedSectionHeaderHeight = 0.0;
        _tableView.estimatedSectionFooterHeight = 0.0;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
    }
    return _tableView;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath];
    cell.textLabel.textColor = [UIColor redColor];
    NSString *text = nil;
    switch (indexPath.row) {
        case 0:
        {
            text = @"小码哥";
            break;
        }
        case 1:
        {
            text = @"ios培训";
            break;
        }
        case 2:
        {
            text = @"微博:吖了个峥";
            break;
        }
        default:
            break;
    }
    cell.textLabel.text = text;
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //图片高度
    CGFloat imageHeight = self.baseView.frame.size.height;
    //图片宽度
    CGFloat imageWidth = [UIScreen mainScreen].bounds.size.width;
    //图片上下偏移量
    CGFloat imageOffsetY = scrollView.contentOffset.y;
    //上移
    if (imageOffsetY < 0) {
        CGFloat totalOffset = imageHeight + ABS(imageOffsetY);
        CGFloat f = totalOffset / imageHeight;
        self.iconImageView.frame = CGRectMake(-(imageWidth * f - imageWidth) * 0.5, imageOffsetY, imageWidth * f, totalOffset);
    }
}

@end