iOS UItableview 图片下拉变大

工程为了实现UITableView 头部图片下拉放大效果,实现了以后想把过程记录下来,方便日后查看
思路:headView加到tableview上,然后通过判断scrollView.contentOffset.y来实现部分效果

效果图:


iOS UItableview 图片下拉变大_第1张图片
UItableview头部下拉.gif

在进入界面的时候设置好隐藏NavigationBar

[self.navigationController setNavigationBarHidden:YES animated:animated];

设置tableview

self.tableview.delegate = self;
    
self.tableview.dataSource = self;
    
[self.tableview addSubview:self.headView];
    
self.tableview.contentInset = UIEdgeInsetsMake(HeadViewHeight, 0, 0, 0);
    
self.tableview.showsVerticalScrollIndicator = NO;

重新写一个NavigationBar

-(UIView*)NavigationBa
{
    view_bar =[[UIView alloc]init];
    
    view_bar.backgroundColor=[UIColor clearColor];
    
    [self.view addSubview: view_bar];
    
    [view_bar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(64);
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(0);
    }];
    
    UIButton * meBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [meBtn setBackgroundImage:[UIImage imageNamed:@"ic_edit"] forState:UIControlStateNormal];
    
    [meBtn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    
    [view_bar addSubview:meBtn];
    
    [meBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.bottom.mas_equalTo(-11);
    }];
    
    self.pageTitle = [[UILabel alloc]init];
    
    self.pageTitle.text = @"我";
    
    self.pageTitle.hidden = YES;
    
    self.pageTitle.textColor = [UIColor clearColor];
    
    [view_bar addSubview:self.pageTitle];
    
    [self.pageTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(meBtn);
        make.centerX.equalTo(view_bar);
    }];
    
    return view_bar;
}

重要的就是这里面的部分代码了

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat yOffset  = scrollView.contentOffset.y;
    if (scrollView == self.tableview) {
        if(yOffset< -(HeadViewHeight+20))
        {
            CGRect frame= self.headView.frame;
            frame.origin.y=yOffset;
            frame.size.height=-yOffset;
            frame.origin.x = (yOffset* SCREENW/183+SCREENW)/2;
            frame.size.width = -yOffset*SCREENW/183;
            self.headView.frame=frame;
            
        }else if(yOffset<-64){
            [view_bar setHidden:NO];
            
            self.pageTitle.hidden = NO;
            
            self.pageTitle.textColor = RGBA(255, 255, 255, (HeadViewHeight+yOffset)/ 100);
            
            view_bar.backgroundColor=RGBA(41,121,246,(HeadViewHeight+yOffset)/ 100);
        }else
        {
            [view_bar setHidden:NO];
            
            self.pageTitle.hidden = NO;
            
            view_bar.backgroundColor=RGBA(41,121,246,1);
        }
    }
}

为了不影响其他页面的NavigationBar

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

新人一枚,写的文章不多,请大家多指教

你可能感兴趣的:(iOS UItableview 图片下拉变大)