🌹简单的Tableview头视图放大🌹

难得回家一次感觉家里的空气都是轻松舒缓的,国庆节快乐!!!

#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];
    
    // 一步到位
    [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   KScreenWidth是宏定义不懂得直接设置为self.view.frome.size.width/height 就可以了
- (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
头视图放大.gif
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容