前言:本文章第一时间出现在一个程序猿的秘密基地专题中,如果您喜欢类似的文章,请您点击一个程序猿的秘密基地进行关注,在以后的日子里与小编共同成长!
#import "ViewController.h"
@interface ViewController ()<UIScrollViewDelegate>
@property(strong,nonatomic)UIImageView *myImageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor blueColor];
UITableView *myTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:myTableView];
myTableView.contentOffset=CGPointMake(0, self.view.frame.size.height*0.4);
myTableView.contentInset=UIEdgeInsetsMake(self.view.frame.size.height*0.4, 0, 0, 0);
//代理要设置
myTableView.delegate=self;
myTableView.backgroundColor=[UIColor redColor];
self.myImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, -self.view.frame.size.height*0.4, self.view.frame.size.width, self.view.frame.size.height*0.4)];
//图片的Mode属性选择UIViewContentModeScaleAspectFill;
self.myImageView.contentMode=UIViewContentModeScaleAspectFill;
self.myImageView.image=[UIImage imageNamed:@"WallPaper_0"];
[myTableView addSubview:self.myImageView];
}
#pragma mark- 当TableView被拖动时就会触发父类ScrollView的方法,可以在这里实现图片的放大及缩小
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGPoint point=scrollView.contentOffset;
NSLog(@"%f",point.y);
if (point.y<0) {
CGRect rect=self.myImageView.frame;
rect.origin.y=point.y;
rect.size.height=-point.y;
self.myImageView.frame=rect;
}
}
@end