仿微博個(gè)人詳情界面

效果

#define HeadViewH 200 // 頂部view高度

#define HeadViewMinH 44? //選項(xiàng)卡高度

#define tabBarH 64? //導(dǎo)航欄高度

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UITableView *tabelView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *headHeightCons;/**頂部view的高度**/

@property (nonatomic,weak) UILabel *label; /** 導(dǎo)航欄文字*/

@property (nonatomic,assign) CGFloat beginOffset ;/** 初始偏移量*/

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// 設(shè)置TableView數(shù)據(jù)源跟代理

self.tabelView.dataSource = self;

self.tabelView.delegate = self;

// 設(shè)置導(dǎo)航欄

[self setUpNavigationBar];

// 不需要添加額外滾動(dòng)區(qū)域

self.automaticallyAdjustsScrollViewInsets = NO;

// 記錄初始偏移量

_beginOffset = -(HeadViewH + HeadViewMinH);

// 設(shè)置tabelView頂部滾動(dòng)區(qū)域 設(shè)置滾動(dòng)區(qū)域會(huì)調(diào)用scrollViewDidScroll:這個(gè)方法

self.tabelView.contentInset = UIEdgeInsetsMake(HeadViewH + tabBarH, 0, 0, 0);

}

// 抽取導(dǎo)航欄設(shè)置

- (void)setUpNavigationBar

{

/**設(shè)置導(dǎo)航條的背景為透明*/

// UIBarMetricsDefault 設(shè)置導(dǎo)航條背景圖片

// 傳遞一個(gè)空的UIImage

[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault];

// 清空導(dǎo)航條的陰影線,傳一個(gè)空的圖片

[self.navigationController.navigationBar setShadowImage:[[UIImage alloc]init]];

/**

設(shè)置頭部標(biāo)題為透明

*/


UILabel *label = [[UILabel alloc]init];

label.text = @"年輕在于折騰";

// 設(shè)置文字顏色

label.textColor = [UIColor colorWithWhite:1 alpha:0];

// 尺寸自適應(yīng)

[label sizeToFit];

_label = label;

// 添加到導(dǎo)航條

[self.navigationItem setTitleView:self.label];

}

#pragma mark UITableViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

/**

*? 圖片的處理

*/

// 獲取當(dāng)前偏移量的Y值

CGFloat curOffsetY = self.tabelView.contentOffset.y;

//偏移量的差值 = tabelView滾動(dòng)了多少

// 當(dāng)前偏移量的值 - 原始偏移量的值

CGFloat delta = curOffsetY - self.beginOffset;

// 求出圖片高度

CGFloat H = HeadViewH - delta;

// 如果圖片高度值小于的導(dǎo)航欄的高度,就讓它跟導(dǎo)航欄等高

if (H < tabBarH) {

H = tabBarH;

}

// 圖片的高度跟隨偏移量的值改變

self.headHeightCons.constant = H ;

/**

*? 導(dǎo)航欄的處理

*/

// 計(jì)算透明度

CGFloat alpha = delta / (HeadViewH - HeadViewMinH);

if (alpha > 1) {

alpha = 0.99;

}

// 設(shè)置導(dǎo)航欄文字

self.label.textColor = [UIColor colorWithWhite:0 alpha:alpha];

// 設(shè)置導(dǎo)航欄背景圖片,根據(jù)當(dāng)前alpha值生成圖片

UIImage *image = [UIImage imageWithColor:[UIColor colorWithWhite:1 alpha:alpha]];

[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

}

圖片的處理

+ (UIImage*)imageWithColor:(UIColor*)color

{

//描述矩形

CGRectrect=CGRectMake(0.0f,0.0f,1.0f,1.0f);

//開(kāi)啟位圖上下文

UIGraphicsBeginImageContext(rect.size);

//獲取位圖上下文

CGContextRefcontext =UIGraphicsGetCurrentContext();

//使用color演示填充上下文

CGContextSetFillColorWithColor(context, [colorCGColor]);

//渲染上下文

CGContextFillRect(context, rect);

//從上下文中獲取圖片

UIImage*theImage =UIGraphicsGetImageFromCurrentImageContext();

//結(jié)束上下文

UIGraphicsEndImageContext();

returntheImage;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容