1.画线
- (void)drawRect:(CGRect)rect {
// Drawing code
//绘一条
//获取上下文 上下文的输出目标就是self[view]
CGContextRef context = UIGraphicsGetCurrentContext();
// 设置线颜色
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1);
// 设置线宽
CGContextSetLineWidth(context, 13);
// 设置线的头尾的样式
CGContextSetLineCap(context, kCGLineCapButt);
// 设置连接点的样式
CGContextSetLineJoin(context, kCGLineJoinRound);
//画一条线
//设置一起点
CGContextMoveToPoint(context, 10, 10);
//设置连线另一个点
CGContextAddLineToPoint(context, 30, 100);
CGContextAddLineToPoint(context, 110, 110);
//画到view [渲染]
CGContextStrokePath(context);
}
2.画矩形
-(void)drawRectangle{
//绘一条
//获取上下文 上下文的输出目标就是self[view]
CGContextRef context = UIGraphicsGetCurrentContext();
// 设置线颜色
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1);
// 设置线宽
//CGContextSetLineWidth(context, 13);
//画一个矩开
//=======第一方法============
// //设置一起点
// CGContextMoveToPoint(context, 10, 10);
// //设置另外三个点
//
// CGContextAddLineToPoint(context, 110, 10);
// CGContextAddLineToPoint(context, 110, 110);
// CGContextAddLineToPoint(context, 10, 110);
// CGContextAddLineToPoint(context, 10, 10);
//=======第一方法============
CGContextAddRect(context, CGRectMake(10, 10, 100, 100));
//画到view [渲染]
//只是画一条,【空心】
//CGContextStrokePath(context);
//填充 【实心】
CGContextFillPath(context);
}
3.画三角形
-(void)drawTriangle{
//获取上下文 上下文的输出目标就是self[view]
CGContextRef context = UIGraphicsGetCurrentContext();
// 设置线颜色
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1);
CGContextMoveToPoint(context, 10, 10);
//设置另外三个点
CGContextAddLineToPoint(context, 110, 10);
CGContextAddLineToPoint(context, 110, 110);
//CGContextAddLineToPoint(context, 10, 10);
//关闭路径
CGContextClosePath(context);
// 渲染
CGContextStrokePath(context);
}
4.画圆
-(void)drawCircle{
//上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//画圈
CGContextAddEllipseInRect(context, CGRectMake(10, 10, 100, 100));
//渲染
CGContextStrokePath(context);
}
5.画弧
-(void)drawArc{
CGContextRef context = UIGraphicsGetCurrentContext();
/**
*x,y 圆心
*radius 半径
*startAngle 画弧的起始位置
*endAngel 画弧的结束位置
* clockwise 0 顺针 1 逆时针
*/
CGContextAddArc(context, 100, 100, 60, 0, M_PI, 1);
CGContextClosePath(context);
//渲染
CGContextStrokePath(context);
//CGContextFillPath(context);
}
6.画扇形
-(void)drawSector{
CGContextRef context = UIGraphicsGetCurrentContext();
//设置一个起点
CGContextMoveToPoint(context, 100, 100);
CGContextAddArc(context, 100, 100, 60, - M_PI_4, - 3 * M_PI_4, 1);
CGContextClosePath(context);
CGContextStrokePath(context);
}
7.画文字
8.画图片
//画图片
UIImage *image = [UIImage imageNamed:@"papa"];
//[image drawAtPoint:CGPointZero];
//[image drawInRect:CGRectMake(10, 10, 50, 50)];
//平铺
[image drawAsPatternInRect:CGRectMake(0, 0, 180, 180)];
9.画进度圈
- (void)drawRect:(CGRect)rect {
// Drawing code
CGFloat textH = 20;
CGFloat textW = 30;
CGFloat viewW = rect.size.width;
CGFloat viewH = rect.size.height;
CGFloat textX = (viewW - textW) * 0.5;
CGFloat textY = (viewH - textH) * 0.5;
// 1.画文字
NSString *text = [NSString stringWithFormat:@"%.2f",self.pregress];
[text drawInRect:CGRectMake(textX, textY, textW, textH) withAttributes:nil];
// 2.画弧
CGContextRef context = UIGraphicsGetCurrentContext();
//半径
CGFloat radius = (viewW - 10) * 0.5;
CGFloat endAngle = self.pregress * 2 * M_PI - M_PI_4;
CGContextAddArc(context, viewW * 0.5, viewH * 0.5, radius, - M_PI_4, endAngle, 0);
CGContextStrokePath(context);
}
-(void)setPregress:(float)pregress{
_pregress = pregress;
//重绘
[self setNeedsDisplay];
}