下拉图片等比例放大,UINavigationController.隐藏下拉显示

#import "MyViewController.h"

#define ImageHight 280.0f@interface MyViewController ()//datas

@property (nonatomic,strong) NSArray *images;

@property (nonatomic,strong) NSArray *titles;

//tableView

@property (nonatomic,strong) UITableView *tableView;

//背景图片

@property (nonatomic,strong) UIImageView *backgroundImageView;

//navigation alp

@property (nonatomic,assign) CGFloat alp;

@end

@implementation MyViewController

#pragma mark life

- (void)viewDidLoad

{

[super viewDidLoad];

//Data

[self loadData];

//UI

[self createUI];

}

#pragma mark Data

- (void)loadData

{

_images = @[@[@"",@"",@""],@[@"",@"",@""],@[@"",@"",@"",@""]];

_titles = @[@[@"我的订单",@"我收藏的折扣",@"我的优惠券"],@[@"我收藏的目的地",@"我的足迹",@"等我点评的目的地"],@[@"我发布的帖子",@"我的问答",@"我的结伴",@"我的讨论组"]];

//刷表

[_tableView reloadData];

}

#pragma mark UI

- (void)createUI

{

//Navigation

self.navigationItem.title = @"我的";

self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.25f green:0.78f blue:0.58f alpha:1.00f];

//刚刚进入页面的时候,将navigationBar设置为透明的

self.navigationController.navigationBar.alpha = 0;

//tableView

_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, RECT.size.width, RECT.size.height) style:UITableViewStyleGrouped];

[self.view addSubview:_tableView];

_tableView.dataSource = self;

_tableView.delegate = self;

//为上啦图片设置偏移量

_tableView.contentInset = UIEdgeInsetsMake(ImageHight, 0, 0, 0);

[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

//设置背景图片

_backgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, -ImageHight, RECT.size.width, ImageHight)];

//这句话也是重点:

//_backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;只有设置为fill才能保证图片被拉伸的时候,左右,上下等比例被拉伸.

_backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;

_backgroundImageView.image = [UIImage imageNamed:@"profileHeader.jpg"];

[_tableView addSubview:_backgroundImageView];

_backgroundImageView.userInteractionEnabled = YES;

//这句话是允许其子视图的的适配

_backgroundImageView.autoresizesSubviews = YES;

//头像设置

UIButton *headerPic = [UIButton buttonWithType:UIButtonTypeCustom];

headerPic.frame = CGRectMake(10, ImageHight/2, 60, 60);

[headerPic setImage:[UIImage imageNamed:@"headerPic"] forState:UIControlStateNormal];

[headerPic addTarget:self action:@selector(headerPicClicked:) forControlEvents:UIControlEventTouchUpInside];

[_backgroundImageView addSubview:headerPic];

headerPic.layer.masksToBounds = YES;

headerPic.layer.cornerRadius = 50;

//tableView向下拉伸的时候,图片放大,子视图的适配,到底部的距离保存不变

headerPic.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;

//用户名设置

UILabel *userName = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(headerPic.frame) +5, CGRectGetMinY(headerPic.frame) + 15, 100, 40)];

[_backgroundImageView addSubview:userName];

userName.text = @"king";

userName.textColor = [UIColor whiteColor];

userName.font = [UIFont systemFontOfSize:18];

[userName sizeToFit];

//这个也是适配,到底部的距离保持不变

userName.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;

}

#pragma mark 头像按钮点击方法

-(void)headerPicClicked:(UIButton *)btn

{

//换头像方法

NSLog(@"点击换头像");

}

#pragma mark backgroundImageView

-(void)scrollViewDidScroll:(UIScrollView *)scrollView

{

//下拉图片变大

//这里是关键.通过拉伸时y坐标的偏移量,来改变被拉伸图片的frame

CGFloat y = scrollView.contentOffset.y;

if (y < -ImageHight)

{

CGRect frame = _backgroundImageView.frame;

frame.origin.y = y;

frame.size.height = -y;

_backgroundImageView.frame = frame;

}

//透明度变化

//y值为负数.

_alp = (y + ImageHight)*4 / ImageHight;

self.navigationController.navigationBar.alpha = _alp;

}

#pragma mark tableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return _titles.count;

}

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

{

if (section == 0)

{

return 3;

}

else if (section == 1)

{

return 3;

}

else

{

return 4;

}

}

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

{

NSString *ident = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ident];

//    cell.imageView = _images[indexPath.section][indexPath.row];

cell.textLabel.text = _titles[indexPath.section][indexPath.row];

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

return cell;

}

#pragma mark tableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

[tableView deselectRowAtIndexPath:indexPath animated:YES];

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

return 15;

}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

{

return 15;

}

你可能感兴趣的:(下拉图片等比例放大,UINavigationController.隐藏下拉显示)