大家可能注意到一些app的tableView的頂部圖片,會(huì)隨著你拉伸而跟著拉伸變大,下面這是我的一些想法
原圖:
效果圖:
下面附上代碼吧,這里的圖片不是添加在tabview的header上
#define SCREEN_W [UIScreen mainScreen].bounds.size.width #define SCREEN_H [UIScreen mainScreen].bounds.size.height #define TOP 200 //頂部預(yù)留 #import "ViewController.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> @property (nonatomic,strong)UITableView *tableV; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self creatTableView]; } - (void)creatTableView { self.automaticallyAdjustsScrollViewInsets = NO; self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H) style:UITableViewStylePlain]; self.tableV.contentInset = UIEdgeInsetsMake(TOP, 0, 0, 0); self.tableV.delegate = self; self.tableV.dataSource = self; //創(chuàng)建頂部圖片 UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, -TOP, SCREEN_W, TOP)]; imageView.tag = 1000; //更改圖片顯示模式 根據(jù)圖片原有尺寸進(jìn)行顯示 將多余部分切除 imageView.contentMode = UIViewContentModeScaleAspectFill; //多余部分隱藏 imageView.clipsToBounds = YES; imageView.image = [UIImage imageNamed:@"pic"]; [self.view addSubview:_tableV]; [self.tableV addSubview:imageView]; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { float offSet = scrollView.contentOffset.y; if (offSet < -200) { UIImageView * tempImageView = (UIImageView*)[self.view viewWithTag:1000]; CGRect f = tempImageView.frame; //保持圖片原點(diǎn)仍為屏幕左上方 f.origin.y = offSet; //保證圖片根據(jù)滑動(dòng)高度拉伸 f.size.height = -offSet; //給圖片重新設(shè)置坐標(biāo) tempImageView.frame = f; } } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 10; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cell = @"cell"; UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:cell]; if (!myCell) { myCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cell]; } myCell.textLabel.text = @"我是 cell"; return myCell; } @end
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注