下拉圖片等比例放大,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 = @[@[@"我的訂單",@"我收藏的折扣",@"我的優(yōu)惠券"],@[@"我收藏的目的地",@"我的足跡",@"等我點評的目的地"],@[@"我發(fā)布的帖子",@"我的問答",@"我的結(jié)伴",@"我的討論組"]];

//刷表

[_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];

//剛剛進(jìn)入頁面的時候,將navigationBar設(shè)置為透明的

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;

//為上啦圖片設(shè)置偏移量

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

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

//設(shè)置背景圖片

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

//這句話也是重點:

//_backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;只有設(shè)置為fill才能保證圖片被拉伸的時候,左右,上下等比例被拉伸.

_backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;

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

[_tableView addSubview:_backgroundImageView];

_backgroundImageView.userInteractionEnabled = YES;

//這句話是允許其子視圖的的適配

_backgroundImageView.autoresizesSubviews = YES;

//頭像設(shè)置

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;

//用戶名設(shè)置

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

{

//下拉圖片變大

//這里是關(guān)鍵.通過拉伸時y坐標(biāo)的偏移量,來改變被拉伸圖片的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值為負(fù)數(shù).

_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;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容