123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- //
- // DetailNavigationBar.m
- // FirstLink
- //
- // Created by jack on 15/6/30.
- // Copyright (c) 2015年 FirstLink. All rights reserved.
- //
- #import "DetailNavigationBar.h"
- #import "FLImageHelper.h"
- @interface DetailNavigationBar ()
- @property (nonatomic, strong) UIView *circleBg;
- @property (nonatomic, strong) UIImageView *arrowImgView;
- @property (nonatomic, strong) UIImageView *bgImgView;
- @end
- @implementation DetailNavigationBar
- @synthesize leftButton = _leftButton;
- @synthesize collectBtn = _collectBtn;
- - (instancetype)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- [self initializeLayout];
- }
- return self;
- }
- - (void)initializeLayout
- {
- self.backgroundColor = [UIColor clearColor];
-
- [self addSubview:self.bgImgView];
- [self addSubview:self.circleBg];
- [self addSubview:self.arrowImgView];
- [self addSubview:self.collectBtn];
- [self addSubview:self.leftButton];
- // [self addSubview:self.bottomLine];
-
- [self.bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.edges.insets(UIEdgeInsetsZero);
- }];
-
- [self.circleBg mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self).offset(20);
- make.centerY.equalTo(self.mas_bottom).offset(- 22);
- make.width.height.equalTo(@30);
- }];
-
- [self.arrowImgView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerX.equalTo(self.circleBg).offset(-1);
- make.centerY.equalTo(self.circleBg);
- }];
-
- [self.collectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(self).offset(- 20);
- make.centerY.equalTo(self.circleBg);
- make.width.height.equalTo(@40);
- }];
-
- [self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
- make.center.equalTo(self.circleBg);
- make.width.height.equalTo(@50);
- }];
- }
- -(void)setBgImgWithAlpha:(CGFloat)alpha
- {
- UIColor *targetColor = [[UIColor blackColor] colorWithAlphaComponent:alpha];
- if (alpha < 0) targetColor = [UIColor clearColor];
- UIImage *image = [FLImageHelper imageWithColor:targetColor];
- self.bgImgView.image = image;
- }
- - (void)setCircleBgWithAlpha:(CGFloat)alpha
- {
- self.circleBg.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:alpha];
- }
- - (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 collectStart = UISCREENWIDTH - 64 + 15;
- CGFloat collectMax = collectStart + 25;
- CGFloat max = 185.0f;
- CGFloat maxPercent = 0.80; // 最大透明度
- CGFloat maxCirclePercent = 0.5; // 圆圈最大透明度
- CGPoint newPoint = [change[@"new"] CGPointValue];
- CGPoint oldPoint = [change[@"old"] CGPointValue];
- 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 setCircleBgWithAlpha:(1 - alphaPercent) * maxCirclePercent];
- }
- if (newPoint.y < 0) return;
-
- // 改变收藏按钮(速度太快的时候)
- if (newPoint.y >= collectMax && oldPoint.y <= collectStart) {
- self.collectBtn.alpha = 1.0f;
- self.collectBtn.tag = 1;
- return;
- }else if (newPoint.y <= collectStart && oldPoint.y >= collectMax){
- self.collectBtn.alpha = 0;
- self.collectBtn.tag = 0;
- return;
- }
-
- if ((newPoint.y >= collectStart && newPoint.y <= collectMax) || (oldPoint.y > collectStart && oldPoint.y < collectMax)) {
- CGFloat collectAlpha = (collectMax - newPoint.y) / (collectMax - collectStart);
- if (self.collectBtn.tag == 0) {
- if (collectAlpha < 0) collectAlpha = 0;
- if (collectAlpha == 0 && oldPoint.y >= collectStart) self.collectBtn.tag = 1;
- self.collectBtn.alpha = 1 - collectAlpha;
- }else if (self.collectBtn.tag == 1){
- if (collectAlpha > 1) collectAlpha = 1;
- if (collectAlpha == 1) self.collectBtn.tag = 0;
- self.collectBtn.alpha = 1 - collectAlpha;
- }
- }
- }
-
- }
- #pragma mark - getter && setter
- - (UIView *)circleBg
- {
- if (_circleBg == nil) {
- _circleBg = [[UIView alloc]init];
- _circleBg.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
- _circleBg.layer.cornerRadius = 15;
- }
- return _circleBg;
- }
- - (UIImageView *)arrowImgView
- {
- if (_arrowImgView == nil) {
- _arrowImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"common_body_back_n"]];
- }
- return _arrowImgView;
- }
- - (UIButton *)collectBtn
- {
- if (_collectBtn == nil) {
- _collectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [_collectBtn setImage:[UIImage imageNamed:@"collectHeartN"] forState:UIControlStateNormal];
- [_collectBtn setImage:[UIImage imageNamed:@"collectHeartH"] forState:UIControlStateSelected];
- _collectBtn.alpha = 0;
- }
- return _collectBtn;
- }
- - (UIButton *)leftButton
- {
- if (_leftButton == nil) {
- _leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
- }
- return _leftButton;
- }
- - (UIImageView *)bgImgView
- {
- if (_bgImgView == nil) {
- _bgImgView = [[UIImageView alloc]init];
- _bgImgView.backgroundColor = [UIColor clearColor];
- }
- return _bgImgView;
- }
- @end
|