tabletableview 上下滑動 依據滑動偏移量,改變headView大小

UITableView 是 UIScrollView 的子類。 
所以 UIScrollView 的代理方法,在UITableView 上同樣能夠得到適用。 

既然如此那麼我們就能夠知道,在表格下拉的過程中,需要讓頭部的圖片能夠有稍微放大的效果出現,我們可以根據滾動視圖滾動監聽事件,通過獲取表格下拉的拉伸量,從而去改變圖片的大小即可!


@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UIImageView *headImageView;//頭部圖片

@property(nonatomic,strong)UITableView *tableView;//列表

@property(nonatomic,strong)NSMutableArray *infoArray;//數據源數組

@end

//屏幕寬、高 宏定義

#define IPHONE_W ([UIScreen mainScreen].bounds.size.width)

#define IPHONE_H ([UIScreen mainScreen].bounds.size.height)

@implementation ViewControllerstatic 

CGFloat kImageOriginHight =300;

- (void)viewDidLoad{  

[super viewDidLoad];  

//將視圖添加到界面上  

[self.view addSubview:self.tableView];  

[self.tableView addSubview:self.headImageView];

}

#pragma mark -- 滾動視圖的代理方法

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

/**  

* 關鍵處理:通過滾動視圖獲取到滾動偏移量從而去改變圖片的變化  

*/  

//獲取滾動視圖y值的偏移量  

CGFloat yOffset = scrollView.contentOffset.y;  

NSLog(@"yOffset===%f",yOffset);  

CGFloat xOffset = (yOffset +kImageOriginHight)/2;  

if(yOffset < -kImageOriginHight) {  

CGRect f =self.headImageView.frame;  

f.origin.y= yOffset ;  

f.size.height= -yOffset;  

f.origin.x= xOffset;  

//int abs(int i); // 處理int類型的取絕對值  

//double fabs(double i); //處理double類型的取絕對值  

//float fabsf(float i); //處理float類型的取絕對值  

f.size.width=IPHONE_W + fabs(xOffset)*2;  

self.headImageView.frame= f;  

}}

#pragma mark -- 表視圖代理

-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{  

return 44;

}

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

return self.infoArray.count;

}

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

static NSString *identify =@"MyCellIndifer";  

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];  

if (!cell) {  

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];  

}  

cell.textLabel.text= [self.infoArray objectAtIndex:indexPath.row];  

return cell;

}#pragma mark -- get 初始化操作

-(UITableView *)tableView{  

if (_tableView == nil) {  

_tableView= [[UITableView alloc]initWithFrame:CGRectMake(0,0,IPHONE_W,IPHONE_H)];  

_tableView.delegate=self;  

_tableView.dataSource=self;  

_tableView.backgroundColor= [UIColor lightGrayColor]; //內容由kImageOriginHight 處開始顯示。  

_tableView.contentInset=UIEdgeInsetsMake(kImageOriginHight,0,0,0);  

}  

return _tableView;

}-(NSMutableArray *)infoArray{  

if (_infoArray == nil) {  

_infoArray = [[NSMutableArray alloc]init];  

for (int i=0; i<40; i++)  

{ [_infoArray addObject:@"這是一個測試!"];  

} }  

return _infoArray;}

-(UIImageView *)headImageView{  

if (_headImageView == nil) {  

_headImageView= [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"111"]];  

_headImageView.frame=CGRectMake(0, -kImageOriginHight,IPHONE_W,kImageOriginHight);  

}  

return _headImageView;

}


同理,可實現通過判斷座標偏移量,改變頭部視圖的形態展示

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章