iOS 绘制电池获取系统电量

前言

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

一 如何使用

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

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,689评论 4 61
  • 【淄博装修】若是年轻的心渴望宁静与安定,即便对于自己的家有1000种想法,最想要的也不过是一个简单温馨的房间。本案...
    谁家的猫啊阅读 1,643评论 0 0
  • 今天进入基金投资的第三课,让我们来学习基金的分类。基金类型很多种,选择基金的原则:没有好坏之分,只有适合不适合。 ...
    昕葉阅读 3,846评论 0 6
  • 吓人的装扮, 盛装的绚烂, 还有鬼斧神工的布点, 热闹的人群, 斯叫的声音, 恐怖弥漫的放纵亡魂, 来一场庆典, ...
    墨度阅读 2,532评论 0 1
  • 在初二的时候,因为中考的改革政策,由于外地户籍的原因,借读生的我再怎么努力,即使是全市第一名,都无缘那三所...
    忆凝阅读 4,535评论 3 8

友情链接更多精彩内容