// 覆盖drawRect方法,你可以在此自定义绘画和动画
- (void)drawRect:(CGRect)rect
{
//An opaque type that represents a Quartz 2D drawing environment.
//一个不透明类型的Quartz 2D绘画环境,相当于一个画布,你可以在上面任意绘画
CGContextRef context = UIGraphicsGetCurrentContext();
/*写文字*/
CGContextSetRGBFillColor (context, 1, 1, 1, 1.0);//设置填充颜色
UIFont *font = [UIFont fontWithName:@"HelveticaNeue-Bold"size:15.0f];
UIColor *redColor = [UIColor redColor];
[self drawInRect:CGRectMake(10, 220, 80, 20) withAttributes: @{NSFontAttributeName:font,NSForegroundColorAttributeName:redColor}];
/*画三角形*/
//只要三个点就行跟画一条线方式一样,把三点连接起来
CGPoint sPoints[3];//坐标点
sPoints[0] =CGPointMake(100, 220);//坐标1
sPoints[1] =CGPointMake(140, 220);//坐标2
sPoints[2] =CGPointMake(120, 160);//坐标3
CGContextAddLines(context, sPoints, 3);//添加线
CGContextClosePath(context);//封起来
CGContextDrawPath(context, kCGPathFillStroke); //根据坐标绘制路径
}
- (void)drawRect:(CGRect)rect
{
//设置背景颜色
[[UIColor clearColor] set];
UIRectFill([self bounds]);
//拿到当前视图准备好的画板
CGContextRef context = UIGraphicsGetCurrentContext();
//利用path进行绘制三角形
CGContextBeginPath(context);//标记
CGContextMoveToPoint(context, self.bounds.size.width / 2, 0);//设置起点
CGContextAddLineToPoint(context, self.bounds.size.width, self.bounds.size.height);
CGContextAddLineToPoint(context, 0, self.bounds.size.height);
CGContextClosePath(context);//路径结束标志,不写默认封闭
[[UIColor redColor] setFill]; //设置填充色
[[UIColor clearColor] setStroke]; //设置边框颜色
CGContextDrawPath(context, kCGPathFillStroke);//绘制路径path
}
//直线
- (void)drawRect:(CGRect)rect
{
//获得处理的上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//指定直线样式
CGContextSetLineCap(context, kCGLineCapSquare);
//直线宽度
CGContextSetLineWidth(context, 2.0);
//设置颜色
CGContextSetRGBStrokeColor(context, 0.314, 0.486, 0.859, 1.0);
//开始绘制
CGContextBeginPath(context);
//画笔移动到点(31,170)
CGContextMoveToPoint(context, 31, 70);
//下一点
CGContextAddLineToPoint(context, 129, 148);
//下一点
CGContextAddLineToPoint(context, 159, 148);
//绘制完成
CGContextStrokePath(context);
}
//矩形
- (void)drawRect:(CGRect)rect{
//创建路径并获取句柄
CGMutablePathRef path = CGPathCreateMutable();
//指定矩形
CGRect rectangle = CGRectMake(10.0f, 10.0f, 200.0f, 300.0f);
//将矩形添加到路径中
CGPathAddRect(path,NULL, rectangle);
//获取上下文
CGContextRef currentContext = UIGraphicsGetCurrentContext();
//将路径添加到上下文
CGContextAddPath(currentContext, path);
//设置矩形填充色
[[UIColor colorWithRed:0.20f green:0.60f blue:0.80f alpha:1.0f] setFill];
//矩形边框颜色
[[UIColor brownColor] setStroke];
//边框宽度
CGContextSetLineWidth(currentContext,5.0f);
//绘制
CGContextDrawPath(currentContext, kCGPathFillStroke);
CGPathRelease(path);
}