美文网首页
iOS 绘制电池获取系统电量

iOS 绘制电池获取系统电量

作者: 杪夏荷月 | 来源:发表于2017-09-02 11:09 被阅读215次

前言

公司的一款新产品要求自己画一个电池并调用,搜索了一下发现画电池和调用的部分基本都是分开的,东西不算难,整理一下方便同行使用,菜鸟一枚,如有不对的地方欢迎指正.

一 如何使用

使用方式比较简单,首先要引用两个文件

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发给你

相关文章

  • iOS 绘制电池获取系统电量

    前言 公司的一款新产品要求自己画一个电池并调用,搜索了一下发现画电池和调用的部分基本都是分开的,东西不算难,整理一...

  • 获取手机的电池信息

    1、获取电池电量 2、获取电池状态 参考地址:http://www.cocoachina.com/ios/2016...

  • Power battery 电池

    电池的消耗以及有关的电池量低的时候提醒1、系统提供的通知方法 [获取电量]2、IOKit的方法 [获取电量]3、检...

  • iOS 如何获取设备相关信息

    1、获取iPhone名称: 2、获取app版本号: 3、// 获取电池电量: 4、// 获取当前系统名称: 5、/...

  • iOS 获取电池电量

    由于项目需要,要求获取iOS手机电池电量的详细值以及变化量等信息。所以做了相关的技术调研,实现的方法有很多。所以写...

  • IOS获取电池准确电量

    找到Mac下IOKit.framework,将IOKit.framework里面的IOPowerSources.h...

  • react-native 获取设备网络适配器MAC地址(reac

    React Native iOS和Android的设备信息,比如["获取应用程序名称","获取设备的电池电量为浮点...

  • iOS电池相关API

    iOS电池相关API iOS系统中关于电池的Api较少,据本人了解目前仅有两种电视相关的Api,一种是电量状态,一...

  • iOS 获取手机设备信息

    一、目录结构: 获取屏幕宽度与高度获取设备版本号获取iPhone名称获取app版本号获取电池电量获取当前系统名称获...

  • Android绘制电池充电效果

    演示效果: 这里是Github代码地址 实现步骤: 1.绘制电池边框2.绘制当前矩形电量3.显示电量文字4.电量连...

网友评论

      本文标题:iOS 绘制电池获取系统电量

      本文链接:https://www.haomeiwen.com/subject/ylwljxtx.html