在这里,博主我用了比较简单的方法实现了下拉放大的功能,基本内容都在代码中有MARK,如果还有什么不明白的可以随时提问
因为考虑到查询这功能的都以新手居多,所以,代码实现的方法是最简单的很多细节都没有写上,而且方法也以让新手看懂为主,大神轻虐啊
#import "ViewController.h"
#define imageHeight 180.0f
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>
{
UITableView *_tab;
UIImageView *_imageV;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_tab = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tab.delegate = self;
_tab.dataSource = self;
//给tableview设定边距
_tab.contentInset = UIEdgeInsetsMake(imageHeight, 0, 0, 0);
[self.view addSubview:_tab];
//创建UIImageView
_imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, -imageHeight, self.view.frame.size.width, imageHeight)];//因为上面设置了边距所有这里的y坐标需要随之改变
_imageV.backgroundColor = [UIColor yellowColor];
_imageV.image = [UIImage imageNamed:@"aiqinhai.jpg"];
//重点(不设置那将只会被纵向拉伸)
//contentMode = UIViewContentModeScaleAspectFill时,高度改变宽度也跟着改变
_imageV.contentMode = UIViewContentModeScaleAspectFill;
// [_tab addSubview:_imageV];//要使用 addSubview 的话 设定的ImageView的高度必须等于或大于图片的高度
[_tab insertSubview:_imageV atIndex:0];
}
#warning -mark 竖图不行
//代理方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//计算CGFloat
CGFloat y = scrollView.contentOffset.y+64;
NSLog(@"%f",y);
if (y<-imageHeight) {
CGRect frame = _imageV.frame;
frame.origin.y = y;
frame.size.height = -y;
_imageV.frame = frame;
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
return cell;
}