ZXVideoPlayerBrightnessView.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // ZXVideoPlayerBrightnessView.m
  3. // ZXVideoPlayer
  4. //
  5. // Created by Shawn on 16/4/21.
  6. // Copyright © 2016年 Shawn. All rights reserved.
  7. //
  8. #import "ZXVideoPlayerBrightnessView.h"
  9. #import "ZXVideoPlayerControlView.h"
  10. static const CGFloat kViewSpacing = 21.0;
  11. static const CGFloat kBrightnessIndicatorAutoFadeOutTimeInterval = 1.0;
  12. @interface ZXVideoPlayerBrightnessView ()
  13. @property (nonatomic, strong) NSMutableArray *blocksArray;
  14. @end
  15. @implementation ZXVideoPlayerBrightnessView
  16. - (void)dealloc
  17. {
  18. [[UIScreen mainScreen] removeObserver:self forKeyPath:@"brightness"];
  19. }
  20. - (instancetype)initWithFrame:(CGRect)frame
  21. {
  22. self = [super initWithFrame:frame];
  23. if (self) {
  24. self.hidden = YES;
  25. self.layer.cornerRadius = 5;
  26. self.layer.masksToBounds = YES;
  27. self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
  28. [self createBrightnessIndicator];
  29. [self configScreenBrightnessObserver];
  30. }
  31. return self;
  32. }
  33. - (void)configScreenBrightnessObserver
  34. {
  35. [[UIScreen mainScreen] addObserver:self
  36. forKeyPath:@"brightness"
  37. options:NSKeyValueObservingOptionNew
  38. context:NULL];
  39. }
  40. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
  41. {
  42. CGFloat brightness = [change[@"new"] floatValue];
  43. [self updateBrightnessIndicator:brightness];
  44. }
  45. - (void)createBrightnessIndicator
  46. {
  47. // 亮度图标
  48. UIImageView *brightnessImageView = [[UIImageView alloc] initWithFrame:CGRectMake((kVideoBrightnessIndicatorViewSide - 50) / 2, kViewSpacing, 50, 50)];
  49. [brightnessImageView setImage:[UIImage imageNamed:@"zx-video-player-brightness"]];
  50. [self addSubview:brightnessImageView];
  51. // 亮度条
  52. self.blocksArray = [NSMutableArray arrayWithCapacity:16];
  53. UIView *blockBackgroundView = [[UIView alloc] initWithFrame:CGRectMake((kVideoVolumeIndicatorViewSide - 105) / 2, 50 + kViewSpacing * 2, 105, 2.75 + 2)];
  54. blockBackgroundView.backgroundColor = [UIColor colorWithRed:0.25f green:0.22f blue:0.21f alpha:0.65];
  55. [self addSubview:blockBackgroundView];
  56. CGFloat margin = 1;
  57. CGFloat blockW = 5.5;
  58. CGFloat blockH = 2.75;
  59. for (int i = 0; i < 16; i++) {
  60. CGFloat locX = i * (blockW + margin) + margin;
  61. UIImageView *blockView = [[UIImageView alloc] init];
  62. blockView.backgroundColor = [UIColor whiteColor];
  63. blockView.frame = CGRectMake(locX, margin, blockW, blockH);
  64. [blockBackgroundView addSubview:blockView];
  65. [self.blocksArray addObject:blockView];
  66. }
  67. }
  68. - (void)updateBrightnessIndicator:(CGFloat)value
  69. {
  70. self.hidden = NO;
  71. // 防止重叠显示
  72. if (self.superview.accessibilityIdentifier) {
  73. ZXVideoPlayerControlView *playerView = (ZXVideoPlayerControlView *)self.superview;
  74. playerView.timeIndicatorView.hidden = YES;
  75. playerView.volumeIndicatorView.hidden = YES;
  76. } else {
  77. self.superview.accessibilityIdentifier = @"";
  78. }
  79. CGFloat stage = 1 / 16.0;
  80. NSInteger level = value / stage;
  81. for (NSInteger i=0; i<self.blocksArray.count; i++) {
  82. UIImageView *img = self.blocksArray[i];
  83. if (i <= level) {
  84. img.hidden = NO;
  85. } else {
  86. img.hidden = YES;
  87. }
  88. }
  89. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(animateHide) object:nil];
  90. [self performSelector:@selector(animateHide) withObject:nil afterDelay:kBrightnessIndicatorAutoFadeOutTimeInterval];
  91. }
  92. - (void)animateHide
  93. {
  94. [UIView animateWithDuration:.3 animations:^{
  95. self.alpha = 0;
  96. } completion:^(BOOL finished) {
  97. self.hidden = YES;
  98. self.alpha = 1;
  99. self.superview.accessibilityIdentifier = nil;
  100. }];
  101. }
  102. @end