ZXVideoPlayerBatteryView.m
2.67 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
//
// ZXVideoPlayerBatteryView.m
// ZXVideoPlayer
//
// Created by Shawn on 16/4/23.
// Copyright © 2016年 Shawn. All rights reserved.
//
#import "ZXVideoPlayerBatteryView.h"
@implementation ZXVideoPlayerBatteryView
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
[self setInitialBatteryLevel];
[self addBatteryObserver];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGFloat borderWidth = 1.0; // 边框线宽
CGFloat margin = 1.0; // 电量填充rect距边框的距离
CGFloat blockWidth = 2.0;
CGFloat blockHeight = 6.0;
[[UIColor whiteColor] set];
// 电池矩形框(self内边缘绘制边框)
UIBezierPath *borderPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(borderWidth / 2, borderWidth / 2, CGRectGetWidth(self.bounds) - borderWidth - blockWidth, CGRectGetHeight(self.bounds) - borderWidth) cornerRadius:3];
borderPath.lineWidth = borderWidth;
[borderPath stroke];
// 电池正极 (只有 topRight && bottomRight 两个角有圆角)
UIBezierPath *blockPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(CGRectGetMaxX(borderPath.bounds) + borderWidth / 2, CGRectGetMidY(borderPath.bounds) - blockHeight / 2, blockWidth, blockHeight) byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomRight cornerRadii:CGSizeMake(blockWidth / 2, blockHeight / 2)];
blockPath.lineWidth = 0.01;
[blockPath fill];
[blockPath stroke];
// 填充电量 (距离边框保留magin单位宽度)
CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(borderWidth + margin, borderWidth + margin, ((CGRectGetWidth(borderPath.bounds) - borderWidth - margin) - margin) * self.batteryLevel, CGRectGetHeight(self.bounds) - (borderWidth + margin) * 2));
}
- (void)setInitialBatteryLevel
{
[UIDevice currentDevice].batteryMonitoringEnabled = YES;
self.batteryLevel = [UIDevice currentDevice].batteryLevel;
}
- (void)addBatteryObserver
{
// or use [[UIDevice currentDevice] addObserver:self forKeyPath:@"batteryLevel" options:NSKeyValueObservingOptionNew context:NULL];
// 以上方法调用次数相对较多,所以修改为仅当值改变时重绘
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onBatteryLevelChanged:) name:UIDeviceBatteryLevelDidChangeNotification object:nil];
}
- (void)onBatteryLevelChanged:(NSNotification *)noti
{
self.batteryLevel = ((UIDevice *)noti.object).batteryLevel;
[self setNeedsDisplay]; // redrawn battery view
}
@end