此方法实在矩形中填充渐变颜色,再将不需要保留的渐变色用背景颜色覆盖。作为记录留存。
#import "DrawCell.h"
#define VIEW_WIDTH self.frame.size.width
#define VIEW_HEIGHT self.frame.size.height
#define CELL_WIDTH (self.frame.size.width-60)/11.0
#define UIColorFromHex(s) [UIColor colorWithRed:(((s & 0xFF0000) >> 16))/255.0 green:(((s &0xFF00) >>8))/255.0 blue:((s &0xFF))/255.0 alpha:1.0]
@implementation DrawCell
-(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
;
}
return self;
}
-(void)addBottomLine{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 1);
UIColor *lineColor = UIColorFromHex(0xa0a0a0);
CGContextSetStrokeColorWithColor(context, lineColor.CGColor);
CGContextMoveToPoint(context, 0, VIEW_HEIGHT-20);
CGContextAddLineToPoint(context, VIEW_WIDTH, VIEW_HEIGHT-20);
CGContextStrokePath(context);
}
-(void)addBottomCoordinate{
UIColor *lineColor = UIColorFromHex(0xa0a0a0);
for (NSInteger i = 0; i < 12; i++) {
CGFloat height = 6;
if (i==0||i==11) {
height = VIEW_HEIGHT-20;
}
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 0.5);
CGContextSetStrokeColorWithColor(context, lineColor.CGColor);
CGContextMoveToPoint(context, 30+i*CELL_WIDTH, VIEW_HEIGHT-20);
CGContextAddLineToPoint(context, 30+i*CELL_WIDTH, VIEW_HEIGHT-20-height);
CGContextStrokePath(context);
}
}
-(void)addBottomCoordinateTextLabel{
for (NSInteger i=0; i<12; i++) {
UILabel *label = [[UILabel alloc] init];
label.textAlignment = NSTextAlignmentCenter;
label.frame = CGRectMake(30+i*CELL_WIDTH-CELL_WIDTH/2, VIEW_HEIGHT-19, CELL_WIDTH, 19);
[self addSubview:label];
label.adjustsFontSizeToFitWidth = YES;
label.minimumScaleFactor = 0.1;
switch (i) {
case 0:
{
label.text = @"Jan";
}
break;
case 1:
{
label.text = @"Jan";
}
break;
case 2:
{
label.text = @"Jan";
}
break;
case 3:
{
label.text = @"Jan";
}
break;
case 4:
{
label.text = @"Jan";
}
break;
case 5:
{
label.text = @"Jan";
}
break;
case 6:
{
label.text = @"Jan";
}
break;
case 7:
{
label.text = @"Jan";
}
break;
case 8:
{
label.text = @"Jan";
}
break;
case 9:
{
label.text = @"Jan";
}
break;
case 10:
{
label.text = @"Jan";
}
break;
case 11:
{
label.text = @"Jan";
}
break;
default:
break;
}
}
}
-(NSMutableArray*)dataArray{
if (_dataArray==nil) {
_dataArray = [[NSMutableArray alloc] init];
for (NSInteger i=0 ;i<13 ;i++) {
CGFloat number = (float)(1+arc4random()%99)/100;
NSLog(@"number is %f",number);
[_dataArray addObject:[NSNumber numberWithFloat:number]];
}
}
return _dataArray;
}
-(void)addDataArrayLine{
CGContextRef context = UIGraphicsGetCurrentContext();
//方法1
// CGFloat locations[2] = {0.0, 1.0};
// CGFloat components[8] = {104.0/255.0, 225.0/255.0, 250.0/255.0, 1.0, 115.0/255.0, 134.0/255.0, 197.0/255.0, 1.0}; // yellow->red
//
// CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
// CGGradientRef gradient = CGGradientCreateWithColorComponents(colorspace, components, locations, 2);
//
// CGPoint startPoint = CGPointMake(VIEW_WIDTH/2, 0);
// CGPoint endPoint = CGPointMake(VIEW_WIDTH/2, VIEW_HEIGHT-20);
// CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
//
// CGGradientRelease(gradient);
// CGColorSpaceRelease(colorspace);
// CGContextSaveGState(context);
//方法2
// UIGraphicsPushContext(context);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat locations[] = {0, 1};
// UIColor *color1 = UIColorFromHex(0x7386c5);
// UIColor *color0 = UIColorFromHex(0x68e1fa);
UIColor *color0 = [UIColor colorWithRed:104.0/255.0 green:225.0/255.0 blue:250.0/255.0 alpha:0.75];
UIColor *color1 = [UIColor colorWithRed:115.0/255.0 green:134.0/255.0 blue:197.0/255.0 alpha:0.75];
NSArray *colors = @[(__bridge id)color0.CGColor, (__bridge id)color1.CGColor];
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef) colors, locations);
CGColorSpaceRelease(colorSpace);
CGPoint startPoint = CGPointMake(VIEW_WIDTH/2, 0);
CGPoint endPoint = CGPointMake(VIEW_WIDTH/2, VIEW_HEIGHT-20);
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
CGGradientRelease(gradient);
// UIGraphicsPopContext();
//方法3
// CIFilter *ciFilter = [CIFilter filterWithName:@"CILinearGradient"];
// CGRect rect = CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT-20);
// UIColor *color0 = UIColorFromHex(0x7386c5);
// UIColor *color1 = UIColorFromHex(0x68e1fa);
// CGPoint startPoint = CGPointMake(0.5, 0);
// CGPoint endPoint = CGPointMake(0.5, 1);
// CIVector *vector0 = [CIVector vectorWithX:rect.size.width * startPoint.x Y:rect.size.height * (1 - startPoint.y)];
// CIVector *vector1 = [CIVector vectorWithX:rect.size.width * endPoint.x Y:rect.size.height * (1 - endPoint.y)];
// [ciFilter setValue:vector0 forKey:@"inputPoint0"];
// [ciFilter setValue:vector1 forKey:@"inputPoint1"];
// [ciFilter setValue:[CIColor colorWithCGColor:color1.CGColor] forKey:@"inputColor0"];
// [ciFilter setValue:[CIColor colorWithCGColor:color0.CGColor] forKey:@"inputColor1"];
//
// CIImage *ciImage = ciFilter.outputImage;
//
// CIContext* con = [CIContext contextWithOptions:nil];
// CGImageRef resultCGImage = [con createCGImage:ciImage
// fromRect:rect];
// UIImage *resultUIImage = [UIImage imageWithCGImage:resultCGImage];
// CGImageRelease(resultCGImage);
// [resultUIImage drawInRect:rect];
//绘制折线图,并且其余不需要显示的部分,用白色覆盖。
CGContextSetLineWidth(context, 1);
// UIColor *lineColor = UIColorFromHex(0x68e1fa);
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextSetRGBFillColor (context, 1.0, 1.0, 1.0, 1.0);
CGFloat fload = [[self.dataArray firstObject] floatValue];
CGContextMoveToPoint(context, 0, (VIEW_HEIGHT-20)*(1.0-fload));
for (NSInteger i =1; i<self.dataArray.count; i++) {
CGFloat height = [[self.dataArray objectAtIndex:i] floatValue];
CGContextAddLineToPoint(context,30+CELL_WIDTH*(i-1), (VIEW_HEIGHT-20)*(1.0-height));
}
CGContextAddLineToPoint(context,30+CELL_WIDTH*(self.dataArray.count-2), VIEW_HEIGHT-20);
CGContextAddLineToPoint(context,VIEW_WIDTH, VIEW_HEIGHT-20);
CGContextAddLineToPoint(context,VIEW_WIDTH, 0);
CGContextAddLineToPoint(context,0, 0);
CGContextAddLineToPoint(context,0, (VIEW_HEIGHT-20)*(1.0-fload));
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke);
CGContextStrokePath(context);
// CGContextRestoreGState(context);
}
-(void)addUnitTextPart{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor (context, 1, 0, 0, 1.0);//设置填充颜色
UIFont *font = [UIFont boldSystemFontOfSize:15.0];//设置
[@"kWh" drawInRect:CGRectMake(0, 0, 40, 20) withAttributes:@{NSFontAttributeName:font}];
[@"kWh" drawInRect:CGRectMake(VIEW_WIDTH-40, 0, 40, 20) withAttributes:@{NSFontAttributeName:font}];
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
NSLog(@"draw view");
//底部label的添加
[self addBottomCoordinateTextLabel];
//顶部画图
[self addDataArrayLine];
//底部灰色的线
[self addBottomLine];
//底部坐标的绘制
[self addBottomCoordinate];
//添加kwh的文字处理
[self addUnitTextPart];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}