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