溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在iOS中使用tableView實現(xiàn)頂部圖片拉伸效果

發(fā)布時間:2021-05-31 17:43:22 來源:億速云 閱讀:213 作者:Leah 欄目:移動開發(fā)

本篇文章為大家展示了怎么在iOS中使用tableView實現(xiàn)頂部圖片拉伸效果,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

#define SCREEN_W [UIScreen mainScreen].bounds.size.width 
#define SCREEN_H [UIScreen mainScreen].bounds.size.height 
#define TOP 200 //頂部預留 
 
#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ù)圖片原有尺寸進行顯示 將多余部分切除 
 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; 
  //保持圖片原點仍為屏幕左上方 
  f.origin.y = offSet; 
  //保證圖片根據(jù)滑動高度拉伸 
  f.size.height = -offSet; 
  //給圖片重新設(shè)置坐標 
  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)容就是怎么在iOS中使用tableView實現(xiàn)頂部圖片拉伸效果,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI