#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;
}