美文网首页
UITableView下拉头视图放大

UITableView下拉头视图放大

作者: 野地里的程序员 | 来源:发表于2017-06-30 09:46 被阅读0次

#import "ViewController.h"

//签署协议

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

// 添加 tableView

@property (nonatomic ,strong) UITableView *dragTableview;

// imageView

@property (nonatomic ,strong) UIImageView *photoImageview;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

//读取plist文件当中的数据

NSString *bb = [[NSBundle mainBundle] pathForResource:@"22" ofType:@"plist"];

NSDictionary *dd= [[NSDictionary alloc] initWithContentsOfFile:bb];

// 一步到位

[self.view addSubview:self.dragTableview];

// 先创建一个头部视图

UIView * headerview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 200)];

// 设置颜色

//    headerview.backgroundColor = [UIColor colorWithRed:245/255.0 green:120/255.0 blue:111/255.0 alpha:0.2];

// 直接加载到内存中去了

self.photoImageview.image = [UIImage imageNamed:@"headerImage1.jpg"];

//将图片添加到头视图

[headerview addSubview:self.photoImageview];

// 设置头部视图

self.dragTableview.tableHeaderView = headerview;

UIView * backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];

[backgroundView addSubview:self.photoImageview];

self.dragTableview.backgroundView = backgroundView;

}

// 拖动的时候,调用这个方法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

NSLog(@"%f",scrollView.contentOffset.y);

// 先取出来 imageView 的 frame

CGRect tempRect = self.photoImageview.frame;

if (scrollView.contentOffset.y > 0) {

// 向上滚动

tempRect.origin.y = -scrollView.contentOffset.y;

// 赋值回来

self.photoImageview.frame = tempRect;

}else {

// 向下 滚动  图片放大(在原来的高度基础上放大) 肯定是跟 contentOffSet 有关系

tempRect.origin.y = 0;

tempRect.size.height = 200 - scrollView.contentOffset.y;

// 把修改后的 frame  赋值回去

self.photoImageview.frame = tempRect;

}

}

#pragma mark --返回组数 return  sections

// 返回组数

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

//

//    return nil;

//}

// 返回行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return 15;

}

// 返回 cell

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString * cellID = @"cellID";

//

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];

if (!cell) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

// 设置颜色

cell.contentView.backgroundColor = [self randomColor];

}

cell.textLabel.text = [NSString stringWithFormat:@"第%ld个",indexPath.row];

return cell;

}

// 懒加载 lazyloading

- (UITableView *)dragTableview {

// 不会重复加载

if (!_dragTableview) {

_dragTableview  = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight) style:UITableViewStylePlain];

_dragTableview.delegate = self;

_dragTableview.dataSource = self;

}

return _dragTableview;

}

- (UIImageView *)photoImageview {

if (!_photoImageview) {

_photoImageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 200)];

// 设置填充方式

_photoImageview.contentMode = UIViewContentModeScaleAspectFill;

}

return _photoImageview;

}

//随机颜色

- (UIColor *)randomColor

{

CGFloat r = arc4random() % 256 / 255.0;

CGFloat g = arc4random() % 256 / 255.0;

CGFloat b = arc4random() % 256 / 255.0;

return [UIColor colorWithRed:r green:g blue:b alpha:0.7];

}

@end

相关文章

网友评论

      本文标题:UITableView下拉头视图放大

      本文链接:https://www.haomeiwen.com/subject/oojbcxtx.html