123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- //
- // FKProductNavigationBar.m
- // FirstLink
- //
- // Created by jack on 16/1/13.
- // Copyright © 2016年 FirstLink. All rights reserved.
- //
- #import "FKProductNavigationBar.h"
- #import "FLImageHelper.h"
- @interface FKProductNavigationBar ()
- @property (nonatomic, strong) UIImageView *bgImgView;
- @property (nonatomic, strong) UIView *bottomLine;
- @end
- @implementation FKProductNavigationBar
- - (instancetype)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- [self addAllSubviews];
- }
- return self;
- }
- - (void)addAllSubviews{
-
- self.backgroundColor = [UIColor clearColor];
-
- [self addSubview:self.bgImgView];
- [self addSubview:self.backBtn];
- [self addSubview:self.shareBtn];
- [self addSubview:self.bottomLine];
-
- [self.bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.edges.insets(UIEdgeInsetsZero);
- }];
-
- [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerX.equalTo(self.mas_left).offset(28);
- make.centerY.equalTo(self.mas_bottom).offset(- 22);
- make.size.mas_equalTo(CGSizeMake(50, 50));
- }];
-
- [self.shareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerX.equalTo(self.mas_right).offset(- 25);
- make.centerY.equalTo(self.backBtn);
- make.width.height.mas_equalTo(50);
- }];
-
- [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.bottom.right.equalTo(self);
- make.height.mas_equalTo(1.0);
- }];
- }
- -(void)setBgImgWithAlpha:(CGFloat)alpha{
-
- UIColor *targetColor = [[UIColor whiteColor] colorWithAlphaComponent:alpha];
- if (alpha < 0) targetColor = [UIColor clearColor];
- UIImage *image = [FLImageHelper imageWithColor:targetColor];
- self.bgImgView.image = image;
- }
- - (void)setBottomLineWithAlpha:(CGFloat)alpha{
- UIColor *targetColor = [UIColorFromRGB(0xe5e5e5) colorWithAlphaComponent:alpha];
- if (alpha < 0) targetColor = [UIColor clearColor];
- self.bottomLine.backgroundColor = targetColor;
- }
- - (void)setStatusBarWithAlphaPerCent:(CGFloat)alphaPercent{
-
- UIStatusBarStyle style = [UIApplication sharedApplication].statusBarStyle;
- UIStatusBarStyle targetSty = UIStatusBarStyleDefault;
-
- if (alphaPercent >= 0 && alphaPercent <= 0.5){
- targetSty = UIStatusBarStyleLightContent; // 白色
- }
- if (targetSty == style) return;
- [[UIApplication sharedApplication] setStatusBarStyle:targetSty animated:NO];
- }
- - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
- {
- if ([keyPath isEqualToString:@"contentOffset"]) {
- CGFloat maxPercent = 1.0f; // 最大透明度
- CGPoint newPoint = [change[@"new"] CGPointValue];
- CGPoint oldPoint = [change[@"old"] CGPointValue];
-
- if ([object isKindOfClass:[UITableView class]]) {
-
- UITableView *tableView = (UITableView *)object;
- CGFloat max = UISCREENWIDTH;
-
- if (tableView.superview.tag == 1) return;
- if (CGPointEqualToPoint(newPoint, oldPoint)) return;
-
- // 改变navBar背景
- CGFloat alphaPercent = newPoint.y / max;
- if (!(alphaPercent >= 1.0f && oldPoint.y >= max)){
- [self setBgImgWithAlpha:MIN(alphaPercent * maxPercent, maxPercent)];
- [self setBottomLineWithAlpha:MIN(alphaPercent * maxPercent, maxPercent)];
- }
- if (newPoint.y < 0) return;
-
- } else if ([object isKindOfClass:[UIScrollView class]]) {
-
- UIScrollView *scrollView = (UIScrollView *)object;
- CGFloat max = CGRectGetHeight(scrollView.bounds) - 44 - 20;
-
- if (CGPointEqualToPoint(newPoint, oldPoint)) return;
- if (newPoint.y == 0) newPoint.y = max;
- // 改变navBar背景
- CGFloat alphaPercent = newPoint.y / max;
- if (!(alphaPercent >= 1.0f && oldPoint.y >= max)){
- [self setBgImgWithAlpha:MIN(alphaPercent * maxPercent, maxPercent)];
- [self setBottomLineWithAlpha:MIN(alphaPercent * maxPercent, maxPercent)];
- }
- }
- }
-
- }
- #pragma mark - property
- - (UIButton *)backBtn{
- if (_backBtn == nil) {
- _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [_backBtn setImage:[UIImage imageNamed:@"fk_common_back"] forState:UIControlStateNormal];
- }
- return _backBtn;
- }
- - (UIButton *)shareBtn{
- if (_shareBtn == nil) {
- _shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [_shareBtn setImage:[UIImage imageNamed:@"product_make_money"] forState:UIControlStateNormal];
- }
- return _shareBtn;
- }
- - (UIImageView *)bgImgView{
- if (_bgImgView == nil) {
- _bgImgView = [[UIImageView alloc]init];
- _bgImgView.backgroundColor = [UIColor clearColor];
- }
- return _bgImgView;
- }
- - (UIView *)bottomLine{
- if (_bottomLine == nil) {
- _bottomLine = [[UIView alloc]init];
- _bottomLine.backgroundColor = [UIColorFromRGB(0xe5e5e5) colorWithAlphaComponent:0.0];
- }
- return _bottomLine;
- }
- @end
|