前言
公司的一款新产品要求自己画一个电池并调用,搜索了一下发现画电池和调用的部分基本都是分开的,东西不算难,整理一下方便同行使用,菜鸟一枚,如有不对的地方欢迎指正.
一 如何使用
使用方式比较简单,首先要引用两个文件
import "IOPSKeys.h"
import "IOPowerSources.h"
只要定义一个View就可以了,电池大小根据需求
- (void)viewDidLoad {
[super viewDidLoad];
self.batteryView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 30, 15)];
[self drawBattery:self.batteryView];
[self.view addSubview:self.batteryView];
}
下面的部分复制可用,也可根据自己的习惯改写.
二 Bezier曲线画电池
- (void)drawBattery: (UIView *)view {
//绘制电池
UIBezierPath *path1 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, view.frame.size.width * 49 / 50, view.frame.size.height) cornerRadius:2];
CAShapeLayer *batteryLayer = [CAShapeLayer layer];
batteryLayer.lineWidth = 1;
batteryLayer.strokeColor = [UIColor blackColor].CGColor;
batteryLayer.fillColor = [UIColor whiteColor].CGColor;
batteryLayer.path = [path1 CGPath];
[view.layer addSublayer:batteryLayer];
UIBezierPath *path2 = [UIBezierPath bezierPath];
[path2 moveToPoint:CGPointMake(view.frame.size.width * 49 / 50 + 1, view.frame.size.height / 3)];
[path2 addLineToPoint:CGPointMake(view.frame.size.width * 49 / 50 + 1, view.frame.size.height * 2 / 3)];
CAShapeLayer *layer2 = [CAShapeLayer layer];
layer2.lineWidth = view.frame.size.width / 50;
layer2.strokeColor = [UIColor blackColor].CGColor;
layer2.fillColor = [UIColor blackColor].CGColor;
layer2.path = [path2 CGPath];
[view.layer addSublayer:layer2];
//绘制电量
UIView *eleView = [[UIView alloc] initWithFrame:CGRectMake(1, 1, 0, view.frame.size.height - 2)];
eleView.layer.cornerRadius = 2;
NSInteger ele = [self getCurrentBatteryLevel];
if (ele <= 20) {
eleView.backgroundColor = [UIColor redColor];
} else {
eleView.backgroundColor = [UIColor greenColor];
}
[view addSubview:eleView];
//绘制数字电量
UILabel *eleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, view.frame.size.width, self.batteryView.frame.size.height)];
eleLabel.backgroundColor = [UIColor clearColor];
eleLabel.font = [UIFont systemFontOfSize:12];
eleLabel.textAlignment = NSTextAlignmentCenter;
eleLabel.text = [NSString stringWithFormat:@"%ld%@", (long)ele, @"%"];
[view addSubview:eleLabel];
[self runProgress:view.frame.size.width * 49 / 5000 * ele - 2 view:eleView];//bleBattery为电池电量
}
三 调用系统电量
- (NSInteger)getCurrentBatteryLevel {
CFTypeRef blob = IOPSCopyPowerSourcesInfo();
CFArrayRef sources = IOPSCopyPowerSourcesList(blob);
CFDictionaryRef pSource = NULL;
const void *psValue;
NSInteger numOfSources = CFArrayGetCount(sources);
if (numOfSources == 0) {
NSLog(@"Error in CFArrayGetCount");
return -1.0f;
}
int curCapacity = 0;
for (int i = 0 ; i < numOfSources ; i++) {
//Returns a CFDictionary with readable information about the specific power source.
pSource = IOPSGetPowerSourceDescription(blob, CFArrayGetValueAtIndex(sources, i));
if (!pSource) {
NSLog(@"Error in IOPSGetPowerSourceDescription");
return -1.0f;
}
psValue = (CFStringRef)CFDictionaryGetValue(pSource, CFSTR(kIOPSNameKey));
int curCapacity = 0;
int maxCapacity = 0;
double percent;
psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSCurrentCapacityKey));
CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &curCapacity);
psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSMaxCapacityKey));
CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &maxCapacity);
percent = ((double)curCapacity/(double)maxCapacity * 100.0f);
}
return -curCapacity;
}
四 填充电量
- (void)runProgress:(NSInteger)progressValue view:(UIView *)view {
[UIView animateWithDuration:1 animations:^{
CGRect frame = view.frame;
frame.size.width = progressValue;
view.frame = frame;
}];
}
五 引用库
需要引用库IOKit.可以自己下载或者加QQ:346991576备注IOKit发给你
网友评论