效果如图:
QQ20170811-180048-HD.gif
//
// ViewController.m
// MineUI
//
// Created by apple on 17/8/11.
// Copyright © 2017年 Wang. All rights reserved.
//
#import "ViewController.h"
#define KScreen_Width [UIScreen mainScreen].bounds.size.width
#define KScreen_Height [UIScreen mainScreen].bounds.size.height
static const CGFloat kBackImageHeight = 280;
static const CGFloat kHeadImageHeight = 80;
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIImageView *backImage;
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIImageView *headImage;
@property (nonatomic, strong) UILabel *userNameLabel;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIBarButtonItem *rightBtn;
@property (nonatomic, strong) UIBarButtonItem *leftBtn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
self.automaticallyAdjustsScrollViewInsets = NO;
[self.view addSubview:self.tableView];
[self.tableView addSubview:self.backImage];
[self.tableView addSubview:self.backView];
[self.backView addSubview:self.headImage];
[self.backView addSubview:self.userNameLabel];
self.navigationItem.titleView = self.titleLabel;
self.navigationItem.leftBarButtonItem = self.leftBtn;
self.navigationItem.rightBarButtonItem = self.rightBtn;
}
-(void)viewWillAppear:(BOOL)animated{
[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}
- (void)viewWillDisappear:(BOOL)animated{
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:nil];
}
#pragma mark - lazy method
- (UITableView *)tableView{
if (!_tableView) {
_tableView = [[UITableView alloc]init];
_tableView.frame = [UIScreen mainScreen].bounds;
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.contentInset = UIEdgeInsetsMake(kBackImageHeight, 0, 0, 0);
}
return _tableView;
}
- (UIImageView *)backImage{
if (!_backImage) {
_backImage = [[UIImageView alloc]init];
_backImage.frame = CGRectMake(0, - kBackImageHeight, KScreen_Width, kBackImageHeight);
_backImage.contentMode = UIViewContentModeScaleAspectFill;
_backImage.image = [UIImage imageNamed:@"background"];
}
return _backImage;
}
- (UIView *)backView{
if (!_backView) {
_backView = [[UIView alloc]init];
_backView.frame = self.backImage.frame;
_backView.backgroundColor = [UIColor clearColor];
}
return _backView;
}
- (UIImageView *)headImage{
if (!_headImage) {
_headImage = [[UIImageView alloc]init];
_headImage.frame = CGRectMake((KScreen_Width - kHeadImageHeight)/2, kHeadImageHeight, kHeadImageHeight, kHeadImageHeight);
_headImage.image = [UIImage imageNamed:@"1.jpg"];
_headImage.contentMode = UIViewContentModeScaleAspectFill;
_headImage.layer.cornerRadius = kHeadImageHeight/2;
_headImage.layer.masksToBounds = YES;
}
return _headImage;
}
- (UILabel *)userNameLabel{
if (!_userNameLabel) {
_userNameLabel = [[UILabel alloc]init];
_userNameLabel.frame = CGRectMake(0, CGRectGetMaxY(self.headImage.frame) + 10, KScreen_Width, 22);
_userNameLabel.text = @"wangyu";
_userNameLabel.font = [UIFont italicSystemFontOfSize:15.f];
_userNameLabel.backgroundColor = [UIColor clearColor];
_userNameLabel.textAlignment = NSTextAlignmentCenter;
}
return _userNameLabel;
}
- (UILabel *)titleLabel{
if (!_titleLabel) {
_titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 30)];
_titleLabel.textColor = [UIColor whiteColor];
_titleLabel.textAlignment = NSTextAlignmentCenter;
_titleLabel.text = @"个人中心";
_titleLabel.alpha = 0;
}
return _titleLabel;
}
- (UIBarButtonItem*)rightBtn{
if (!_rightBtn) {
UIButton *rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 30)];
[rightBtn setTitle:@"设置" forState:UIControlStateNormal];
rightBtn.titleLabel.font = [UIFont systemFontOfSize:13];
[rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[rightBtn addTarget:self action:@selector(rightBtnAction) forControlEvents:UIControlEventTouchUpInside];
_rightBtn=[[UIBarButtonItem alloc]initWithCustomView:rightBtn];
}
return _rightBtn;
}
- (UIBarButtonItem*)leftBtn{
if (!_leftBtn) {
UIButton *leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 30)];
[leftBtn setTitle:@"返回" forState:UIControlStateNormal];
leftBtn.titleLabel.font = [UIFont systemFontOfSize:13];
[leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[leftBtn addTarget:self action:@selector(leftBtnAction) forControlEvents:UIControlEventTouchUpInside];
_leftBtn = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
}
return _leftBtn;
}
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:@"haha%ld",indexPath.row];
return cell;
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if ([scrollView isKindOfClass:[UITableView class]]) {
CGFloat yOffset = scrollView.contentOffset.y;
CGRect newFrame = self.backImage.frame;
if (yOffset < -kBackImageHeight) {
newFrame.origin.y = yOffset;
newFrame.size.height = -yOffset;
}
self.backImage.frame = newFrame;
CGFloat alpha = (yOffset + kBackImageHeight)/kBackImageHeight;
[self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[[UIColor cyanColor] colorWithAlphaComponent:alpha]] forBarMetrics:UIBarMetricsDefault];
self.titleLabel.alpha = alpha;
alpha = fabs(alpha);
alpha = fabs(1 - alpha);
alpha = alpha < 0.2 ? 0 : alpha - 0.2;
self.backView.alpha = alpha;
}
}
#pragma mark - action
- (void)rightBtnAction{
NSLog(@"点击右边按钮了");
}
- (void)leftBtnAction{
NSLog(@"点击左边边按钮了");
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - private method
- (UIImage *)imageWithColor:(UIColor *)color{
// 描述矩形
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
// 开启位图上下文
UIGraphicsBeginImageContext(rect.size);
// 获取位图上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 使用color演示填充上下文
CGContextSetFillColorWithColor(context, [color CGColor]);
// 渲染上下文
CGContextFillRect(context, rect);
// 从上下文中获取图片
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
// 结束上下文
UIGraphicsEndImageContext();
return theImage;
}
@end
网友评论