ios tableview下拉頭部圖片變大

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *mytableview;
@property(nonatomic,retain)NSMutableArray *listenArray;
@property(nonatomic,retain)UIImageView *imageView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.listenArray = [NSMutableArray array];
    
    self.mytableview=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, 375 , 667  - 49-64) style:UITableViewStylePlain];
    [self.view addSubview:self.mytableview];
    //    [_tableView release];
    self.mytableview.delegate = self;
    self.mytableview.dataSource = self;
    self.mytableview.contentInset = UIEdgeInsetsMake(220 , 0, 0, 0);
    self.mytableview.separatorStyle = 0;

    //相對於0點,圖片座標應該是(0,-200)
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -220 , self.view.frame.size.width, 220 )];
    self.imageView.image = [UIImage imageNamed:@"afaded9fb75cfcf92bab35347d8e8ab8.jpg"];
    //設置imageView高度改變時寬度也跟着改變
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.mytableview addSubview:self.imageView];
  }
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    cell.textLabel.text=@"123";
     return cell;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //剛開始y的偏移量初始值就是-220
    //    //NSLog(@"y1 === %f",scrollView.contentOffset.y);
    CGFloat y = scrollView.contentOffset.y ;//第一次是-220
    //    //NSLog(@"y2 === %f",y);
    
    if (y < -220 ) {
        CGRect frame = self.imageView.frame;
        frame.origin.y = y;//imageView的frame是不斷往上偏移
        frame.size.height =  -y;//tablview向下偏移了多少,高度就增長多少
        self.imageView.frame = frame;
    }
    
}
相關文章
相關標籤/搜索