ZFVolumeBrightnessView.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // ZFVolumeBrightnessView.m
  3. // ZFPlayer
  4. //
  5. // Copyright (c) 2016年 任子丰 ( http://github.com/renzifeng )
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. #import "ZFVolumeBrightnessView.h"
  25. #import <MediaPlayer/MediaPlayer.h>
  26. #import "ZFUtilities.h"
  27. #if __has_include(<ZFPlayer/ZFPlayer.h>)
  28. #import <ZFPlayer/ZFPlayer.h>
  29. #else
  30. #import "ZFPlayer.h"
  31. #endif
  32. @interface ZFVolumeBrightnessView ()
  33. @property (nonatomic, strong) UIProgressView *progressView;
  34. @property (nonatomic, strong) UIImageView *iconImageView;
  35. @property (nonatomic, assign) ZFVolumeBrightnessType volumeBrightnessType;
  36. @property (nonatomic, strong) MPVolumeView *volumeView;
  37. @end
  38. @implementation ZFVolumeBrightnessView
  39. - (instancetype)initWithFrame:(CGRect)frame {
  40. self = [super initWithFrame:frame];
  41. if (self) {
  42. [self addSubview:self.iconImageView];
  43. [self addSubview:self.progressView];
  44. [self hideTipView];
  45. }
  46. return self;
  47. }
  48. - (void)dealloc {
  49. [self addSystemVolumeView];
  50. }
  51. - (void)layoutSubviews {
  52. [super layoutSubviews];
  53. CGFloat min_x = 0;
  54. CGFloat min_y = 0;
  55. CGFloat min_w = 0;
  56. CGFloat min_h = 0;
  57. CGFloat min_view_w = self.frame.size.width;
  58. CGFloat min_view_h = self.frame.size.height;
  59. CGFloat margin = 10;
  60. min_x = margin;
  61. min_w = 20;
  62. min_h = min_w;
  63. min_y = (min_view_h-min_h)/2;
  64. self.iconImageView.frame = CGRectMake(min_x, min_y, min_w, min_h);
  65. min_x = CGRectGetMaxX(self.iconImageView.frame) + margin;
  66. min_h = 2;
  67. min_y = (min_view_h-min_h)/2;
  68. min_w = min_view_w - min_x - margin;
  69. self.progressView.frame = CGRectMake(min_x, min_y, min_w, min_h);
  70. self.layer.cornerRadius = min_view_h/2;
  71. self.layer.masksToBounds = YES;
  72. }
  73. - (void)hideTipView {
  74. [UIView animateWithDuration:0.5 animations:^{
  75. self.alpha = 0;
  76. } completion:^(BOOL finished) {
  77. self.hidden = YES;
  78. }];
  79. }
  80. /// 添加系统音量view
  81. - (void)addSystemVolumeView {
  82. [self.volumeView removeFromSuperview];
  83. }
  84. /// 移除系统音量view
  85. - (void)removeSystemVolumeView {
  86. [[UIApplication sharedApplication].keyWindow addSubview:self.volumeView];
  87. }
  88. - (void)updateProgress:(CGFloat)progress withVolumeBrightnessType:(ZFVolumeBrightnessType)volumeBrightnessType {
  89. if (progress >= 1) {
  90. progress = 1;
  91. } else if (progress <= 0) {
  92. progress = 0;
  93. }
  94. self.progressView.progress = progress;
  95. self.volumeBrightnessType = volumeBrightnessType;
  96. UIImage *playerImage = nil;
  97. if (volumeBrightnessType == ZFVolumeBrightnessTypeVolume) {
  98. if (progress == 0) {
  99. playerImage = ZFPlayer_Image(@"ZFPlayer_muted");
  100. } else if (progress > 0 && progress < 0.5) {
  101. playerImage = ZFPlayer_Image(@"ZFPlayer_volume_low");
  102. } else {
  103. playerImage = ZFPlayer_Image(@"ZFPlayer_volume_high");
  104. }
  105. } else if (volumeBrightnessType == ZFVolumeBrightnessTypeumeBrightness) {
  106. if (progress >= 0 && progress < 0.5) {
  107. playerImage = ZFPlayer_Image(@"ZFPlayer_brightness_low");
  108. } else {
  109. playerImage = ZFPlayer_Image(@"ZFPlayer_brightness_high");
  110. }
  111. }
  112. self.iconImageView.image = playerImage;
  113. self.hidden = NO;
  114. self.alpha = 1;
  115. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hideTipView) object:nil];
  116. [self performSelector:@selector(hideTipView) withObject:nil afterDelay:1.5];
  117. }
  118. - (void)setVolumeBrightnessType:(ZFVolumeBrightnessType)volumeBrightnessType {
  119. _volumeBrightnessType = volumeBrightnessType;
  120. if (volumeBrightnessType == ZFVolumeBrightnessTypeVolume) {
  121. self.iconImageView.image = ZFPlayer_Image(@"ZFPlayer_volume");
  122. } else {
  123. self.iconImageView.image = ZFPlayer_Image(@"ZFPlayer_brightness");
  124. }
  125. }
  126. - (UIProgressView *)progressView {
  127. if (!_progressView) {
  128. _progressView = [[UIProgressView alloc] init];
  129. _progressView.progressTintColor = [UIColor whiteColor];
  130. _progressView.trackTintColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.4];;
  131. }
  132. return _progressView;
  133. }
  134. - (UIImageView *)iconImageView {
  135. if (!_iconImageView) {
  136. _iconImageView = [UIImageView new];
  137. }
  138. return _iconImageView;
  139. }
  140. - (MPVolumeView *)volumeView {
  141. if (!_volumeView) {
  142. _volumeView = [[MPVolumeView alloc] init];
  143. _volumeView.frame = CGRectMake(-1000, -1000, 100, 100);
  144. }
  145. return _volumeView;
  146. }
  147. @end