No Description

FKProductNavigationBar.m 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // FKProductNavigationBar.m
  3. // FirstLink
  4. //
  5. // Created by jack on 16/1/13.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKProductNavigationBar.h"
  9. #import "FLImageHelper.h"
  10. @interface FKProductNavigationBar ()
  11. @property (nonatomic, strong) UIImageView *bgImgView;
  12. @property (nonatomic, strong) UIView *bottomLine;
  13. @end
  14. @implementation FKProductNavigationBar
  15. - (instancetype)initWithFrame:(CGRect)frame
  16. {
  17. self = [super initWithFrame:frame];
  18. if (self) {
  19. [self addAllSubviews];
  20. }
  21. return self;
  22. }
  23. - (void)addAllSubviews{
  24. self.backgroundColor = [UIColor clearColor];
  25. [self addSubview:self.bgImgView];
  26. [self addSubview:self.backBtn];
  27. [self addSubview:self.shareBtn];
  28. [self addSubview:self.bottomLine];
  29. [self.bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
  30. make.edges.insets(UIEdgeInsetsZero);
  31. }];
  32. [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  33. make.centerX.equalTo(self.mas_left).offset(28);
  34. make.centerY.equalTo(self.mas_bottom).offset(- 22);
  35. make.size.mas_equalTo(CGSizeMake(50, 50));
  36. }];
  37. [self.shareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  38. make.centerX.equalTo(self.mas_right).offset(- 25);
  39. make.centerY.equalTo(self.backBtn);
  40. make.width.height.mas_equalTo(50);
  41. }];
  42. [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
  43. make.left.bottom.right.equalTo(self);
  44. make.height.mas_equalTo(1.0);
  45. }];
  46. }
  47. -(void)setBgImgWithAlpha:(CGFloat)alpha{
  48. UIColor *targetColor = [[UIColor whiteColor] colorWithAlphaComponent:alpha];
  49. if (alpha < 0) targetColor = [UIColor clearColor];
  50. UIImage *image = [FLImageHelper imageWithColor:targetColor];
  51. self.bgImgView.image = image;
  52. }
  53. - (void)setBottomLineWithAlpha:(CGFloat)alpha{
  54. UIColor *targetColor = [UIColorFromRGB(0xe5e5e5) colorWithAlphaComponent:alpha];
  55. if (alpha < 0) targetColor = [UIColor clearColor];
  56. self.bottomLine.backgroundColor = targetColor;
  57. }
  58. - (void)setStatusBarWithAlphaPerCent:(CGFloat)alphaPercent{
  59. UIStatusBarStyle style = [UIApplication sharedApplication].statusBarStyle;
  60. UIStatusBarStyle targetSty = UIStatusBarStyleDefault;
  61. if (alphaPercent >= 0 && alphaPercent <= 0.5){
  62. targetSty = UIStatusBarStyleLightContent; // 白色
  63. }
  64. if (targetSty == style) return;
  65. [[UIApplication sharedApplication] setStatusBarStyle:targetSty animated:NO];
  66. }
  67. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  68. {
  69. if ([keyPath isEqualToString:@"contentOffset"]) {
  70. CGFloat maxPercent = 1.0f; // 最大透明度
  71. CGPoint newPoint = [change[@"new"] CGPointValue];
  72. CGPoint oldPoint = [change[@"old"] CGPointValue];
  73. if ([object isKindOfClass:[UITableView class]]) {
  74. UITableView *tableView = (UITableView *)object;
  75. CGFloat max = UISCREENWIDTH;
  76. if (tableView.superview.tag == 1) return;
  77. if (CGPointEqualToPoint(newPoint, oldPoint)) return;
  78. // 改变navBar背景
  79. CGFloat alphaPercent = newPoint.y / max;
  80. if (!(alphaPercent >= 1.0f && oldPoint.y >= max)){
  81. [self setBgImgWithAlpha:MIN(alphaPercent * maxPercent, maxPercent)];
  82. [self setBottomLineWithAlpha:MIN(alphaPercent * maxPercent, maxPercent)];
  83. }
  84. if (newPoint.y < 0) return;
  85. } else if ([object isKindOfClass:[UIScrollView class]]) {
  86. UIScrollView *scrollView = (UIScrollView *)object;
  87. CGFloat max = CGRectGetHeight(scrollView.bounds) - 44 - 20;
  88. if (CGPointEqualToPoint(newPoint, oldPoint)) return;
  89. if (newPoint.y == 0) newPoint.y = max;
  90. // 改变navBar背景
  91. CGFloat alphaPercent = newPoint.y / max;
  92. if (!(alphaPercent >= 1.0f && oldPoint.y >= max)){
  93. [self setBgImgWithAlpha:MIN(alphaPercent * maxPercent, maxPercent)];
  94. [self setBottomLineWithAlpha:MIN(alphaPercent * maxPercent, maxPercent)];
  95. }
  96. }
  97. }
  98. }
  99. #pragma mark - property
  100. - (UIButton *)backBtn{
  101. if (_backBtn == nil) {
  102. _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  103. [_backBtn setImage:[UIImage imageNamed:@"fk_common_back"] forState:UIControlStateNormal];
  104. }
  105. return _backBtn;
  106. }
  107. - (UIButton *)shareBtn{
  108. if (_shareBtn == nil) {
  109. _shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  110. [_shareBtn setImage:[UIImage imageNamed:@"product_make_money"] forState:UIControlStateNormal];
  111. }
  112. return _shareBtn;
  113. }
  114. - (UIImageView *)bgImgView{
  115. if (_bgImgView == nil) {
  116. _bgImgView = [[UIImageView alloc]init];
  117. _bgImgView.backgroundColor = [UIColor clearColor];
  118. }
  119. return _bgImgView;
  120. }
  121. - (UIView *)bottomLine{
  122. if (_bottomLine == nil) {
  123. _bottomLine = [[UIView alloc]init];
  124. _bottomLine.backgroundColor = [UIColorFromRGB(0xe5e5e5) colorWithAlphaComponent:0.0];
  125. }
  126. return _bottomLine;
  127. }
  128. @end