No Description

DetailNavigationBar.m 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // DetailNavigationBar.m
  3. // FirstLink
  4. //
  5. // Created by jack on 15/6/30.
  6. // Copyright (c) 2015年 FirstLink. All rights reserved.
  7. //
  8. #import "DetailNavigationBar.h"
  9. #import "FLImageHelper.h"
  10. @interface DetailNavigationBar ()
  11. @property (nonatomic, strong) UIView *circleBg;
  12. @property (nonatomic, strong) UIImageView *arrowImgView;
  13. @property (nonatomic, strong) UIImageView *bgImgView;
  14. @end
  15. @implementation DetailNavigationBar
  16. @synthesize leftButton = _leftButton;
  17. @synthesize collectBtn = _collectBtn;
  18. - (instancetype)initWithFrame:(CGRect)frame
  19. {
  20. self = [super initWithFrame:frame];
  21. if (self) {
  22. [self initializeLayout];
  23. }
  24. return self;
  25. }
  26. - (void)initializeLayout
  27. {
  28. self.backgroundColor = [UIColor clearColor];
  29. [self addSubview:self.bgImgView];
  30. [self addSubview:self.circleBg];
  31. [self addSubview:self.arrowImgView];
  32. [self addSubview:self.collectBtn];
  33. [self addSubview:self.leftButton];
  34. // [self addSubview:self.bottomLine];
  35. [self.bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
  36. make.edges.insets(UIEdgeInsetsZero);
  37. }];
  38. [self.circleBg mas_makeConstraints:^(MASConstraintMaker *make) {
  39. make.left.equalTo(self).offset(20);
  40. make.centerY.equalTo(self.mas_bottom).offset(- 22);
  41. make.width.height.equalTo(@30);
  42. }];
  43. [self.arrowImgView mas_makeConstraints:^(MASConstraintMaker *make) {
  44. make.centerX.equalTo(self.circleBg).offset(-1);
  45. make.centerY.equalTo(self.circleBg);
  46. }];
  47. [self.collectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  48. make.right.equalTo(self).offset(- 20);
  49. make.centerY.equalTo(self.circleBg);
  50. make.width.height.equalTo(@40);
  51. }];
  52. [self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
  53. make.center.equalTo(self.circleBg);
  54. make.width.height.equalTo(@50);
  55. }];
  56. }
  57. -(void)setBgImgWithAlpha:(CGFloat)alpha
  58. {
  59. UIColor *targetColor = [[UIColor blackColor] colorWithAlphaComponent:alpha];
  60. if (alpha < 0) targetColor = [UIColor clearColor];
  61. UIImage *image = [FLImageHelper imageWithColor:targetColor];
  62. self.bgImgView.image = image;
  63. }
  64. - (void)setCircleBgWithAlpha:(CGFloat)alpha
  65. {
  66. self.circleBg.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:alpha];
  67. }
  68. - (void)setStatusBarWithAlphaPerCent:(CGFloat)alphaPercent
  69. {
  70. UIStatusBarStyle style = [UIApplication sharedApplication].statusBarStyle;
  71. UIStatusBarStyle targetSty = UIStatusBarStyleDefault;
  72. if (alphaPercent >= 0 && alphaPercent <= 0.5){
  73. targetSty = UIStatusBarStyleLightContent; // 白色
  74. }
  75. if (targetSty == style) return;
  76. [[UIApplication sharedApplication] setStatusBarStyle:targetSty animated:NO];
  77. }
  78. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  79. {
  80. if ([keyPath isEqualToString:@"contentOffset"]) {
  81. CGFloat collectStart = UISCREENWIDTH - 64 + 15;
  82. CGFloat collectMax = collectStart + 25;
  83. CGFloat max = 185.0f;
  84. CGFloat maxPercent = 0.80; // 最大透明度
  85. CGFloat maxCirclePercent = 0.5; // 圆圈最大透明度
  86. CGPoint newPoint = [change[@"new"] CGPointValue];
  87. CGPoint oldPoint = [change[@"old"] CGPointValue];
  88. if (CGPointEqualToPoint(newPoint, oldPoint)) return;
  89. // 改变navBar背景
  90. CGFloat alphaPercent = newPoint.y / max;
  91. if (!(alphaPercent >= 1.0f && oldPoint.y >= max)){
  92. [self setBgImgWithAlpha:MIN(alphaPercent * maxPercent, maxPercent)];
  93. [self setCircleBgWithAlpha:(1 - alphaPercent) * maxCirclePercent];
  94. }
  95. if (newPoint.y < 0) return;
  96. // 改变收藏按钮(速度太快的时候)
  97. if (newPoint.y >= collectMax && oldPoint.y <= collectStart) {
  98. self.collectBtn.alpha = 1.0f;
  99. self.collectBtn.tag = 1;
  100. return;
  101. }else if (newPoint.y <= collectStart && oldPoint.y >= collectMax){
  102. self.collectBtn.alpha = 0;
  103. self.collectBtn.tag = 0;
  104. return;
  105. }
  106. if ((newPoint.y >= collectStart && newPoint.y <= collectMax) || (oldPoint.y > collectStart && oldPoint.y < collectMax)) {
  107. CGFloat collectAlpha = (collectMax - newPoint.y) / (collectMax - collectStart);
  108. if (self.collectBtn.tag == 0) {
  109. if (collectAlpha < 0) collectAlpha = 0;
  110. if (collectAlpha == 0 && oldPoint.y >= collectStart) self.collectBtn.tag = 1;
  111. self.collectBtn.alpha = 1 - collectAlpha;
  112. }else if (self.collectBtn.tag == 1){
  113. if (collectAlpha > 1) collectAlpha = 1;
  114. if (collectAlpha == 1) self.collectBtn.tag = 0;
  115. self.collectBtn.alpha = 1 - collectAlpha;
  116. }
  117. }
  118. }
  119. }
  120. #pragma mark - getter && setter
  121. - (UIView *)circleBg
  122. {
  123. if (_circleBg == nil) {
  124. _circleBg = [[UIView alloc]init];
  125. _circleBg.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
  126. _circleBg.layer.cornerRadius = 15;
  127. }
  128. return _circleBg;
  129. }
  130. - (UIImageView *)arrowImgView
  131. {
  132. if (_arrowImgView == nil) {
  133. _arrowImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"common_body_back_n"]];
  134. }
  135. return _arrowImgView;
  136. }
  137. - (UIButton *)collectBtn
  138. {
  139. if (_collectBtn == nil) {
  140. _collectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  141. [_collectBtn setImage:[UIImage imageNamed:@"collectHeartN"] forState:UIControlStateNormal];
  142. [_collectBtn setImage:[UIImage imageNamed:@"collectHeartH"] forState:UIControlStateSelected];
  143. _collectBtn.alpha = 0;
  144. }
  145. return _collectBtn;
  146. }
  147. - (UIButton *)leftButton
  148. {
  149. if (_leftButton == nil) {
  150. _leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
  151. }
  152. return _leftButton;
  153. }
  154. - (UIImageView *)bgImgView
  155. {
  156. if (_bgImgView == nil) {
  157. _bgImgView = [[UIImageView alloc]init];
  158. _bgImgView.backgroundColor = [UIColor clearColor];
  159. }
  160. return _bgImgView;
  161. }
  162. @end