iOS开发——绘图电池按百分比显示
2016-05-13 10:36
471 查看
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
//
// 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自定义NavigationBar
- 一个丝滑的全屏滑动返回手势
- 自学 iOS 开发的一些经验
- iOS多线程编程知多少
- iOS本地通知
- iOS 正则表达式
- IOS 控制台颜色
- iOS远程推送原理及实现过程
- iOS开发——自定义进度圆环
- iOS 打包上传蒲公英安装失败及证书选择相关事项
- iOS 使用科大讯飞技术实现语音转文字(语音听写)
- 轻松学习之三——IMP指针的作用
- iOS 静变量static、全局变量extern、局部变量、实例变量
- iOS中self.和下划线的区别
- iOS开发之 Realm框架基础知识详解
- 《Motion Design for iOS》(十七)
- iOS Device Types(设备型号:iPhone Model)
- iOS 调试问题记录(三)
- iOS引导界面与主界面之间切换
- iOS Provisioning Profile(Certificate)与Code Signing详解