ZFVolumeBrightnessView.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. @interface ZFVolumeBrightnessView ()
  28. @property (nonatomic, strong) UIProgressView *progressView;
  29. @property (nonatomic, strong) UIImageView *iconImageView;
  30. @property (nonatomic, assign) ZFVolumeBrightnessType volumeBrightnessType;
  31. @property (nonatomic, strong) MPVolumeView *volumeView;
  32. @end
  33. @implementation ZFVolumeBrightnessView
  34. - (instancetype)initWithFrame:(CGRect)frame {
  35. self = [super initWithFrame:frame];
  36. if (self) {
  37. [self addSubview:self.iconImageView];
  38. [self addSubview:self.progressView];
  39. [self hideTipView];
  40. }
  41. return self;
  42. }
  43. - (void)dealloc {
  44. [self addSystemVolumeView];
  45. }
  46. - (void)layoutSubviews {
  47. [super layoutSubviews];
  48. CGFloat min_x = 0;
  49. CGFloat min_y = 0;
  50. CGFloat min_w = 0;
  51. CGFloat min_h = 0;
  52. CGFloat min_view_w = self.frame.size.width;
  53. CGFloat min_view_h = self.frame.size.height;
  54. CGFloat margin = 10;
  55. min_x = margin;
  56. min_w = 20;
  57. min_h = min_w;
  58. min_y = (min_view_h-min_h)/2;
  59. self.iconImageView.frame = CGRectMake(min_x, min_y, min_w, min_h);
  60. min_x = CGRectGetMaxX(self.iconImageView.frame) + margin;
  61. min_h = 2;
  62. min_y = (min_view_h-min_h)/2;
  63. min_w = min_view_w - min_x - margin;
  64. self.progressView.frame = CGRectMake(min_x, min_y, min_w, min_h);
  65. self.layer.cornerRadius = min_view_h/2;
  66. self.layer.masksToBounds = YES;
  67. }
  68. - (void)hideTipView {
  69. [UIView animateWithDuration:0.5 animations:^{
  70. self.alpha = 0;
  71. } completion:^(BOOL finished) {
  72. self.hidden = YES;
  73. }];
  74. }
  75. /// 添加系统音量view
  76. - (void)addSystemVolumeView {
  77. [self.volumeView removeFromSuperview];
  78. }
  79. /// 移除系统音量view
  80. - (void)removeSystemVolumeView {
  81. [[UIApplication sharedApplication].keyWindow addSubview:self.volumeView];
  82. }
  83. - (void)updateProgress:(CGFloat)progress withVolumeBrightnessType:(ZFVolumeBrightnessType)volumeBrightnessType {
  84. if (progress >= 1) {
  85. progress = 1;
  86. } else if (progress <= 0) {
  87. progress = 0;
  88. }
  89. self.progressView.progress = progress;
  90. self.volumeBrightnessType = volumeBrightnessType;
  91. UIImage *playerImage = nil;
  92. if (volumeBrightnessType == ZFVolumeBrightnessTypeVolume) {
  93. if (progress == 0) {
  94. playerImage = ZFPlayer_Image(@"ZFPlayer_muted");
  95. } else if (progress > 0 && progress < 0.5) {
  96. playerImage = ZFPlayer_Image(@"ZFPlayer_volume_low");
  97. } else {
  98. playerImage = ZFPlayer_Image(@"ZFPlayer_volume_high");
  99. }
  100. } else if (volumeBrightnessType == ZFVolumeBrightnessTypeumeBrightness) {
  101. if (progress >= 0 && progress < 0.5) {
  102. playerImage = ZFPlayer_Image(@"ZFPlayer_brightness_low");
  103. } else {
  104. playerImage = ZFPlayer_Image(@"ZFPlayer_brightness_high");
  105. }
  106. }
  107. self.iconImageView.image = playerImage;
  108. self.hidden = NO;
  109. self.alpha = 1;
  110. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hideTipView) object:nil];
  111. [self performSelector:@selector(hideTipView) withObject:nil afterDelay:1.5];
  112. }
  113. - (void)setVolumeBrightnessType:(ZFVolumeBrightnessType)volumeBrightnessType {
  114. _volumeBrightnessType = volumeBrightnessType;
  115. if (volumeBrightnessType == ZFVolumeBrightnessTypeVolume) {
  116. self.iconImageView.image = ZFPlayer_Image(@"ZFPlayer_volume");
  117. } else {
  118. self.iconImageView.image = ZFPlayer_Image(@"ZFPlayer_brightness");
  119. }
  120. }
  121. - (UIProgressView *)progressView {
  122. if (!_progressView) {
  123. _progressView = [[UIProgressView alloc] init];
  124. _progressView.progressTintColor = [UIColor whiteColor];
  125. _progressView.trackTintColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.4];;
  126. }
  127. return _progressView;
  128. }
  129. - (UIImageView *)iconImageView {
  130. if (!_iconImageView) {
  131. _iconImageView = [UIImageView new];
  132. }
  133. return _iconImageView;
  134. }
  135. - (MPVolumeView *)volumeView {
  136. if (!_volumeView) {
  137. _volumeView = [[MPVolumeView alloc] init];
  138. _volumeView.frame = CGRectMake(-1000, -1000, 100, 100);
  139. }
  140. return _volumeView;
  141. }
  142. @end