头文件
//
// triangleView.h
// fileUpload
//
// Created by liuyinghui on 16/1/4.
// Copyright © 2016年 CESF. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface triangleView : UIView
@property UIColor* color;
@property UIColor* backColor;
@end
//
// triangleView.m
//
// Created by liuyinghui on 16/1/4.
// Copyright © 2016年 CESF. All rights reserved.
//
#import "triangleView.h"
@implementation triangleView
@synthesize color;
- (NSMutableArray *) changeUIColorToRGB:(UIColor *)color
{
UIColor *localColor=color;
NSMutableArray *RGBStrValueArr = [[NSMutableArray alloc] init];
//获得RGB值描述
NSString *RGBValue = [NSString stringWithFormat:@"%@",localColor];
//将RGB值描述分隔成字符串
NSArray *RGBArr = [RGBValue componentsSeparatedByString:@" "];
//获取红色值
int r = [[RGBArr objectAtIndex:1] intValue] * 255;
NSNumber * RGBFloatValueR=[NSNumber numberWithInt:r];
[RGBStrValueArr addObject:RGBFloatValueR];
//获取绿色值
int g = [[RGBArr objectAtIndex:2] intValue] * 255;
NSNumber *RGBFloatValueG=[NSNumber numberWithInt:g];
[RGBStrValueArr addObject:RGBFloatValueG];
//获取蓝色值
int b = [[RGBArr objectAtIndex:3] intValue] * 255;
NSNumber *RGBFloatValueB=[NSNumber numberWithInt:b];
[RGBStrValueArr addObject:RGBFloatValueB];
//返回保存RGB值的数组
return RGBStrValueArr;
}
- (void)drawTriangle
{
//设置背景颜色
[self.backColor set];
UIRectFill([self bounds]);
CGContextRef context = UIGraphicsGetCurrentContext();
CGPoint sPoints[3];//坐标点
sPoints[0] =CGPointMake(0, self.frame.size.height);//坐标1
sPoints[1] =CGPointMake(self.frame.size.width, 0);//坐标2
sPoints[2] =CGPointMake(self.frame.size.width-8,self.frame.size.height);//坐标3
NSMutableArray *RGBArray=[self changeUIColorToRGB:[UIColor greenColor]];
NSNumber *rValue=[RGBArray objectAtIndex:0];
NSNumber *gValue=[RGBArray objectAtIndex:1];
NSNumber *bValue=[RGBArray objectAtIndex:2];
CGContextSetRGBStrokeColor(context, [rValue floatValue],[gValue floatValue],[bValue floatValue],1);
CGContextAddLines(context, sPoints, 3);//添加线
CGContextClosePath(context);//封起来
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);//填充颜色
CGContextDrawPath(context, kCGPathFillStroke); //根据坐标绘制路径
}
- (void)drawRect:(CGRect)rect{
[super drawRect:rect];
[self drawTriangle];
}
@end
- (void)drawTriangle
{ triangleView *triangle=[[triangleView alloc] initWithFrame:CGRectMake(0, 23, 88, 25)];
triangle.backColor=self.view.backgroundColor;
[self.view addSubview:triangle];
}