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

 

相关文章:

  • 2021-11-17
  • 2021-05-31
  • 2021-05-05
  • 2021-11-27
  • 2022-12-23
  • 2022-02-22
  • 2022-03-02
  • 2022-01-24
猜你喜欢
  • 2021-05-26
  • 2021-12-14
  • 2021-09-19
  • 2021-07-01
  • 2022-12-23
  • 2021-06-21
  • 2022-02-01
相关资源
相似解决方案