zoukankan      html  css  js  c++  java
  • iOS开发——绘图电池按百分比显示

      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

  • 相关阅读:
    用 Java 写一个折半查找?
    阐述 ArrayList、Vector、LinkedList 的存储性能和特性?
    简述一下面向对象的”六原则一法则”?
    如何通过反射调用对象的方法?
    用 Java 写一个单例类?
    事务的 ACID 是指什么?
    如何通过反射创建对象?
    如何通过反射获取和设置对象私有字段的值?
    JDBC 中如何进行事务处理?
    获得一个类的类对象有哪些方式?
  • 原文地址:https://www.cnblogs.com/yyt-hehe-yyt/p/5488519.html
Copyright © 2011-2022 走看看