ios CCRectangleBlock.m

//
//  RectangleBlock.h
//  PhoneManager
//
//


#import <UIKit/UIKit.h>




@interface CCRectangleBlock : UIView {
    NSUInteger blocksNumber;
    NSArray *blocksPercentage;
    NSArray *blocksColor;
}
@property(nonatomic, retain) NSArray *blocksPercentage;
@property(nonatomic, retain) NSArray *blocksColor;


- (void)setBlocksAndColors:(NSUInteger)number blocks:(NSArray*)percentage colors:(NSArray*)color;


@end




@interface Battery : UIView{
@private
    CCRectangleBlock *level;
    UIView *head;
    
    UILabel *percentage;
}
@property(nonatomic, retain) CCRectangleBlock *level;
@property(nonatomic, retain) UIView *head;
@property(nonatomic, retain) UILabel *percentage;


@end

 

//
//  RectangleBlock.m
//  PhoneManager
//


#import "CCRectangleBlock.h"
#import "CCSystem.h"




@implementation CCRectangleBlock
@synthesize blocksPercentage, blocksColor;


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
//        self.backgroundColor = [UIColor redColor];
    }
    return self;
}


- (void)setBlocksAndColors:(NSUInteger)number blocks:(NSArray*)percentage colors:(NSArray*)color
{
    NSAssert(number == [percentage count] && number == [color count], @"error");
    blocksNumber = number;
    self.blocksPercentage = percentage;
    self.blocksColor = color;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, 200, 200, 100, 0.5);
//    self.backgroundColor = [UIColor grayColor];
//    NSLog(@"frame: %@, bounds: %@", NSStringFromCGRect([self frame]), NSStringFromCGRect([self bounds]));
//    CGContextFillRect(context, CGRectOffset([self frame], -20, -10));
    CGContextFillRect(context, [self bounds]);
    CGFloat totalWidth = self.frame.size.width;
    CGFloat totalHeight = self.frame.size.height;
    CGFloat startPoint = 0;
    for (NSUInteger i = 0; i != blocksNumber; ++i) {
        UIColor *color = [blocksColor objectAtIndex:i];
        const CGFloat *colorComponents = CGColorGetComponents([color CGColor]);
        CGFloat blockWidth = totalWidth * [[blocksPercentage objectAtIndex:i] floatValue];
        CGContextSetRGBFillColor(context, colorComponents[0], colorComponents[1], colorComponents [2], colorComponents[3]);
        CGContextFillRect(context, CGRectMake(startPoint, 0, blockWidth, totalHeight));
        startPoint += blockWidth;
    }
    CGContextSetStrokeColorWithColor(context, [[UIColor blueColor] CGColor]);
    CGContextStrokeRect(context, [self bounds]);
//    CGContextStrokeRect(context, CGRectOffset([self frame], -20, -10));
//    CGContextStrokePath(context);
}




- (void)dealloc
{
    [blocksColor release];
    [blocksPercentage release];
    [super dealloc];
}


@end


@implementation Battery
@synthesize level;
@synthesize head;
@synthesize percentage;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
        level = [[CCRectangleBlock alloc] initWithFrame:CGRectMake(0, 0, frame.size.width * 0.95, frame.size.height)];
        head = [[UIView alloc] initWithFrame:CGRectMake(frame.size.width * 0.95 + 1, frame.size.height * 0.35, frame.size.width * 0.05 - 1, frame.size.height * 0.3)];
        self.level.backgroundColor = [UIColor grayColor];
        self.head.backgroundColor = [UIColor grayColor];
        float volume = getBatteryLevel();
        [level setBlocksAndColors:1 blocks:[NSArray arrayWithObjects:[NSNumber numberWithFloat:volume], nil] colors:[NSArray arrayWithObjects:[UIColor greenColor], nil]];
        NSLog(@"battery monitoring enabled: %d, volume is: %f", [[UIDevice currentDevice]isBatteryMonitoringEnabled], volume);
        
        volume = getBatteryLevel();
        
        percentage = [[UILabel alloc] initWithFrame:CGRectZero];
        percentage.center = [self center];
        percentage.frame = CGRectMake(self.bounds.size.width / 4, 10, self.bounds.size.width / 2, self.bounds.size.height - 20);
//        percentage.opaque = NO;
        percentage.textAlignment = UITextAlignmentCenter;
        percentage.backgroundColor = [UIColor clearColor];
        percentage.text = [NSString stringWithFormat:@"%d%%", [[NSNumber numberWithFloat:100 * volume] intValue]];
        [self addSubview:level];
        [self addSubview:head];
        [self insertSubview:percentage atIndex:1];
        
        [level release];
        [head release];
        [percentage release];
        
    }
    return self;
}


- (void)drawRect:(CGRect)rect
{
    
    
//    [level drawRect:rect];
}
//- (void)batteryLevelDidChange:(NSNotification*) notification
//{
//    float volume = getBatteryLevel();
//    [level setBlocksAndColors:1 blocks:[NSArray arrayWithObjects:[NSNumber numberWithFloat:volume], nil] colors:[NSArray arrayWithObjects:[UIColor greenColor], nil]];
//    NSLog(@"battery monitoring enabled: %d, volume is: %f", [[UIDevice currentDevice]isBatteryMonitoringEnabled], volume);
//    percentage.text = [NSString stringWithFormat:@"%d%%", [[NSNumber numberWithFloat:volume] intValue]];
//    [self setNeedsDisplay];
//}


- (void)dealloc
{
    [level release];
    [head release];
    [super dealloc];
}


@end


微风不燥,阳光正好,你就像风一样经过这里,愿你停留的片刻温暖舒心。

我是程序员小迷(致力于C、C++、Java、Kotlin、Android、Shell、JavaScript、TypeScript、Python等编程技术的技巧经验分享),若作品对您有帮助,请关注、分享、点赞、收藏、在看、喜欢,您的支持是我们为您提供帮助的最大动力。

欢迎关注。助您在编程路上越走越好!

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-15 00:50:01       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-15 00:50:01       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-15 00:50:01       58 阅读
  4. Python语言-面向对象

    2024-07-15 00:50:01       69 阅读

热门阅读

  1. Spring之常见注解

    2024-07-15 00:50:01       20 阅读
  2. Golang 后端面经

    2024-07-15 00:50:01       21 阅读
  3. 印度标普基金关门,继续套利美元债LOF

    2024-07-15 00:50:01       20 阅读
  4. 基于深度学习的点云平滑

    2024-07-15 00:50:01       19 阅读
  5. 【网络编程】poll函数

    2024-07-15 00:50:01       18 阅读
  6. 19. 删除链表的倒数第 N 个结点

    2024-07-15 00:50:01       16 阅读
  7. PyMysql error : Packet Sequence Number Wrong - got 1 expected 0

    2024-07-15 00:50:01       20 阅读
  8. TCP和UDP知识点

    2024-07-15 00:50:01       21 阅读
  9. 【C++】指针学习 知识点总结+代码记录

    2024-07-15 00:50:01       19 阅读
  10. 游戏开发面试题1

    2024-07-15 00:50:01       17 阅读
  11. 利率债与信用债的区别及其与债券型基金的关系

    2024-07-15 00:50:01       19 阅读
  12. 域名信息的收集

    2024-07-15 00:50:01       19 阅读