导航栏全透明下拉放大图片

导航栏全透明下拉放大图片





@interface ViewController () <UITableViewDataSource, UITableViewDelegate>


@property (nonatomic, strong) UITableView *table;

@property (nonatomic, strong) UIImageView *image;

@property (nonatomic, assign) CGFloat offset;

@end

@implementation ViewController

static NSString *const reuseId = @"cell";

#pragma mark ~~~~~~~~~~ 页面加载 ~~~~~~~~~~

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];


    self.image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 3)];

    self.image.image = [UIImage imageNamed:@"image"];

    [self.view addSubview:self.image];


    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, XPF_TopHeight, self.view.frame.size.width, self.view.frame.size.height - XPF_TopHeight) style:UITableViewStylePlain];

    self.table.dataSource = self;

    self.table.delegate = self;

    self.table.backgroundColor = [UIColor clearColor];


    self.table.contentInset = UIEdgeInsetsMake(self.view.frame.size.height/3 - XPF_TopHeight, 0, 0, 0);

    [self.view addSubview: self.table];


    self.offset = self.table.contentOffset.y;


    [self.table registerClass:[UITableViewCell class] forCellReuseIdentifier:reuseId];


}

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];


    //去掉透明后导航栏下边的黑边

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


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

}

#pragma mark ~~~~~~~~~~ TableViewDataSource ~~~~~~~~~~

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 20;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];

    cell.textLabel.text = [NSString stringWithFormat:@"第%zdrow",indexPath.row];

    return cell;

}

#pragma mark ~~~~~~~~~~ ScrollViewDelegate ~~~~~~~~~~

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    CGFloat y = scrollView.contentOffset.y;

    XPFLog(@"%f---%f",y,self.offset);

    CGRect frame = self.image.frame;

    if (y > self.offset) {

        XPFLog(@"向上");

        self.title = @"";

        frame.origin.y = self.offset-y;

        if (y>=0) {

            frame.origin.y = self.offset;

            self.title = @"标题";

        }

        self.image.frame = frame;

    }

    // tableView设置偏移时不能立马获取他的偏移量,所以一开始获取的offset值为0

    else if (self.offset == 0) return;

    else {

        XPFLog(@"向下");

        CGFloat x = self.offset - y;

        frame = CGRectMake(-x/2, -x/2, self.view.frame.size.width + x, self.view.frame.size.height/3+x);

        self.image.frame = frame;

    }

}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容