1、在DrawLine.h文件中提供了一个改变电池电量数值的接口

//

//  DrawLine.h

//  Demo-draw2

//

//  Created by yyt on 16/5/11.

//  Copyright © 2016年 yyt. All rights reserved.

//

#import <UIKit/UIKit.h>

@interface DrawLine : UIView

@property(nonatomic,assign) NSInteger currentNum;

@end

  2、在DrawLine.m文件中

//

//  DrawLine.m

//  Demo-draw2

//

//  Created by yyt on 16/5/11.

//  Copyright © 2016年 yyt. All rights reserved.

//

#import "DrawLine.h"

@implementation DrawLine

- (void)drawRect:(CGRect)rect {

CGContextRef bgContextRef = UIGraphicsGetCurrentContext();

CGRect frame = CGRectMake(10, 10, 40, 20);

CGContextAddRect(bgContextRef, frame);

CGContextSetLineWidth(bgContextRef, 2);

[commonBgColor setStroke];

CGContextStrokePath(bgContextRef);

CGContextMoveToPoint(bgContextRef, 50, 20);

CGContextAddLineToPoint(bgContextRef, 54, 20);

CGContextSetLineWidth(bgContextRef, 6);

CGContextStrokePath(bgContextRef);

CGContextMoveToPoint(bgContextRef, 10, 20);

CGContextAddLineToPoint(bgContextRef, 10+_currentNum*40/100, 20);

CGContextSetLineWidth(bgContextRef, 20);

CGContextStrokePath(bgContextRef);

}

@end

  3、在需要用的地方ViewController.m文件中

  我这里是写了个死数据,真正做项目的话,就需要在电量值发生改变的时候,修改currentNum值,然后setNeedsDisplay一下。

//

//  ViewController.m

//  Demo-draw2

//

//  Created by yyt on 16/5/11.

//  Copyright © 2016年 yyt. All rights reserved.

//

#import "ViewController.h"

#import "DrawLine.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor = [UIColor lightGrayColor];

DrawLine *lineView = [[DrawLine alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];

lineView.backgroundColor = [UIColor whiteColor];

  lineView.currentNum = 87;

[self.view addSubview:lineView];

}

@end

iOS开发——绘图电池按百分比显示的更多相关文章

  1. iOS开发(1) WebView和HTML 显示

    iOS 7 已经release了.现在学习iOS开发还是非常热门的.到处也有些团队在寻找iOS开发的人才. 那么,iOS开发.....省略了1万字.... HTML5 +CSS3+JS...再省略1万 ...

  2. iOS开发--绘图教程

    本文是<Programming iOS5>中Drawing一章的翻译,考虑到主题完整性,翻译版本中加入了一些书中未涉及到的内容.希望本文能够对你有所帮助. 本文由海水的味道翻译整理,转载请 ...

  3. iOS开发 绘图详解

    Quartz概述 Quartz是Mac OS X的Darwin核心之上的绘图层,有时候也认为是CoreGraphics.共有两种部分组成   Quartz Compositor,合成视窗系统,管理和合 ...

  4. iOS开发遇到的错误 -- Label显示多行文字导致宽度和高度的问题

    Label的宽度问题 注意:UILabel下面需要设置preferredMaxLayoutWidth ,设置了autolayout和numberofline的UIlabel才显示多行 label宽度的 ...

  5. iOS开发之应用首次启动显示用户引导

    这个功能的重点就是在如何判断应用是第一次启动的. 其实很简单 我们只需要在一个类里面写好用户引导页面  基本上都是使用UIScrollView 来实现, 新建一个继承于UIViewController ...

  6. [iOS 开发]UITableView第一行显示不完全

    造成这个问题的原因可能有两个: 1. UITableView的contentOffset属性的改变: 2. MJRefresh调用两次headerEndRefreshing会造成刷新后UITableV ...

  7. IOS开发之无法选择模拟器显示NO Scheme

    1. 不是 文件冲突的 看这个链接https://blog.csdn.net/sanpintian/article/details/7377365 2.文件冲突的  打开工程文件. 打开 直接 搜索 ...

  8. iOS开发-- 设置UIButton的文字显示位置、字体的大小、字体的颜色

    btn.frame = CGRectMake(x, y, width, height); [btn setTitle: @"search" forState: UIControlS ...

  9. ios开发之--系统控件显示中文

    虽然一直知道X-code肯定提供有语言本地化的设置地方,但是一直也做个记录,有些时候的汉化,还是需要使用代码去控制,键盘的右下角.navagiton的return使用代码修改,调用系统相机时,也是出现 ...

随机推荐

  1. TabControl选项卡

    <Grid> <TabControl Name="tabControl1"> <TabItem Name="tabItem1"&g ...

  2. Android OpenGL ES(三)OpenGL ES API 命名习惯 .

    OpenGL ES是个跨平台的3D图形开发包规范,最常见的实现是采用C语言实现的,Android OpenGL ES 实现上是使用Java 语言对底层的C接口进行了封装,因此在android.open ...

  3. js获取当前日期与星期

    var currentDate = new Date(); var weekday = ["星期日", "星期一", "星期二", &quo ...

  4. HDU 2802 F(N)(简单题,找循环解)

    题目链接 F(N) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  5. action参数绑定

    thinkPHP支持操作方法的参数绑定功能 action参数通过直接绑定URL中的变量作为操作方法的参数,可以简化方法的定义甚至路由的简析. 原理是把URL的中参数(不包括模块,控制器和操作名)和控制 ...

  6. jsp导入外部样式

    在博客园遇到一位朋友,她将我的代码下载下去,运行的时候,jsp页面的样式不存在,不会显示出来. 这里就将我之前写的SpringSpringmvcMybatis做一些修改. jdk1.8 加入了两个ja ...

  7. select标签 样式 及文本有空格

    <s:select name="codeid" id="codeid" multiple="false"  list="#s ...

  8. laravel 报错 mcrypt_decrypt(): Key of size 11 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported

    修改app/config/app.php文件 将key设置成长度为16,24,32的字符串

  9. CF 476 div2 C

    http://www.codeforces.com/contest/476/problem/C   C. Dreamoon and Sums time limit per test 1.5 secon ...

  10. js-转大小写

    .toLowerCase()转小写.toUpperCase();转大写