工作生活

iOS UITableView 实现下拉头部缩放效果

2019-07-03  本文已影响0人  小明好爱学习

//可直接复制

#import "ViewController.h"
#define kOPHEIGHT 247 // 核心尺寸参数,强烈建议跟原背景图的高度一致
@interface ViewController ()<UITableViewDelegate>
@property(nonatomic,strong) UIView * tableHeadView;
@property(nonatomic,strong) UITableView * tableView;
@end

@implementation HNOtherPeopleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTabelHeadView];
    [self initTableView];
}

-(void)initTableView{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height)];
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    self.tableView.contentInset = UIEdgeInsetsMake(kOPHEIGHT, 0, 0, 0);//向上偏移
    [self.tableView addSubview:_tableHeadView];
}

-(void)initTabelHeadView{
    _tableHeadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, kOPHEIGHT)];
    //背景图
    UIImageView*imgTableHeadViewBg= [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, kOPHEIGHT)];
    imgTableHeadViewBg.image= [UIImage imageNamed:@"背景1"];
    imgTableHeadViewBg.tag = 201;
    imgTableHeadViewBg.contentMode = UIViewContentModeScaleAspectFill;
    [_tableHeadView addSubview:imgTableHeadViewBg];
}

#pragma mark UITableViewDelegate
//头部视图放大效果,及titleView透明度效果
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGPoint point = scrollView.contentOffset;
    if (point.y < -kOPHEIGHT) {
        CGRect rect = [_tableHeadView viewWithTag:201].frame;
        rect.origin.y = point.y;
        rect.size.height = -point.y;
        [_tableHeadView viewWithTag:201].frame = rect;
    }
}

@end
上一篇下一篇

猜你喜欢

热点阅读