UITableView下拉头视图放大

#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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,118评论 3 38
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,586评论 1 14
  • 改变自己,先从头发变起,随意的剪了短发,这几天肯定会有不习惯,学着适应吧,尝试,真的需要改变了,为自己,大气,释然...
    日落绚阅读 101评论 0 0
  • ** 以下内容并非本人原创,只是网友提供,转载不详,此处仅作备忘 ** 这样的写法可以保证库每次都是最新的,但也带...
    CalvinNing阅读 3,591评论 0 3
  • 常羡人间琢玉郎,天应乞与点酥娘。 尽道清歌传皓齿,风起,雪飞炎海变清凉。 万里归来颜愈少,微笑,笑时犹带岭梅香。 ...
    达鲁花赤阅读 206评论 0 0