#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;
//创建顶部图片
uiimageview * imageview = [[uiimageview alloc]initwithframe:cgrectmake(0, -top, screen_w, top)];
imageview.tag = 1000;
//更改图片显示模式 根据图片原有尺寸进行显示 将多余部分切除
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;
//保证图片根据滑动高度拉伸
f.size.height = -offset;
//给图片重新设置坐标
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
|