属性记录头图滑动前的位置
@property (nonatomic,strong)UIView *mHeadView;
@property (nonatomic,assign)CGFloat mHeadViewHeight;
@property (nonatomic,assign)CGRect mHeadViewFrame;
@property (nonatomic,assign)CGPoint mHeadViewCenter;
将头图放到self.view上,列表放到self.view上。
//头图的高度 self.mHeadViewHeight = UIScreenWidth*0.9; [self.view addSubview:weakself.mHeadView]; [self.view addSubview:weakself.mTableView]; //记录一下滑动前的位置 self.mHeadViewFrame = self.mHeadView.frame; self.mHeadViewCenter = self.mHeadView.center; //设置列表上偏移头图的高度 self.mTableView.contentInset = UIEdgeInsetsMake(self.mHeadViewHeight, 0, 0, 0); [self.mTableView setContentOffset:CGPointMake(0, -self.mHeadViewHeight)];
关键代码,处理滚动逻辑
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{ if(scrollView == _mTableView){ CGFloat height = scrollView.contentOffset.y; if(height < -self.mHeadViewHeight){ CGFloat scale = (-height)/self.mHeadViewHeight; scale = MAX(scale, 1); self.mHeadView.frame = CGRectMake(0, 0, self.mHeadViewFrame.size.width * scale, self.mHeadViewFrame.size.height*scale); }else{ self.mHeadView.frame = self.mHeadViewFrame; } self.mHeadView.center = self.mHeadViewCenter; self.mHeadView.top = 0; } }
懒加载,头图视图
-(UIView *)mHeadView{ if(!_mHeadView){ _mHeadView = [[UIView alloc]init]; _mHeadView.frame = CGRectMake(0, 0, UIScreenWidth, self.mHeadViewHeight); _mHeadView.backgroundColor = [UIColor whiteColor]; } return _mHeadView; }