一:
1.单线
1).初始化
CGContextRef ctx =UIGraphicsGetCurrentContext();
2).起始点坐标,第一个为上下文,后两个是坐标,起点坐标
CGContextMoveToPoint(ctx,15, 50);
3)终点坐标,第一个为上下文,后两个是坐标,终点坐标
CGContextAddLineToPoint(ctx,150, 50);
4).设置线的width
CGContextSetLineWidth(ctx,10);
5).设置线的颜色
CGContextSetRGBStrokeColor(ctx,0, 1,0, 1);
6).画线,将线展示出来
CGContextStrokePath(ctx);
2.多线
7).初始化
CGContextRef ctx =UIGraphicsGetCurrentContext();
8).设置线的宽度
CGContextSetLineWidth(ctx,5);
9).设置链接的样式
CGContextSetLineJoin(ctx,kCGLineJoinRound);
10).设置链接端的样式
CGContextSetLineCap(ctx,kCGLineCapRound);
11).定义点数组
CGPoint points[] = {CGPointMake(50,50),CGPointMake(50,100),CGPointMake(100,200),CGPointMake(50,50),};
12).添加点,第一个为上下文,第二个为点的数组,第三个是有效的点数
CGContextAddLines(ctx, points, 4);
13).画线显示出来
CGContextStrokePath(ctx);
3.点线
14).初始化
CGContextRef ctx =UIGraphicsGetCurrentContext();
15).第一个参数上下问,第二个参数phase在第二次绘制时由第一个参数减去后在继续绘制,第三个参数为长度数组,第四个参数为有效数量
CGFloat lengths[] = {10,3,20};
CGContextSetLineDash(ctx, 0, lengths, 4);
起始点
CGContextMoveToPoint(ctx,50, 150);
终点
CGContextAddLineToPoint(ctx,70, 600);
16).画线显示
CGContextStrokePath(ctx);
4.设置矩形
17).设置矩形的颜色,边框的颜色
[[UIColorblueColor]setStroke];
18).设置填充的颜色
[[UIColororangeColor]setFill];
UIRectFill(CGRectMake(100,100, 200, 300));
19).设置矩形的大小
UIRectFrame(CGRectMake(100,100, 200, 300));
或者如下:
初始化
CGContextRef ctx =UIGraphicsGetCurrentContext();
20).设置填充的颜色
CGContextSetRGBFillColor(ctx,0, 1,0, 1);
21).设置线条的颜色
CGContextSetRGBStrokeColor(ctx,0, 1,1, 1);
22).设置线的宽度
CGContextSetLineWidth(ctx,10);
23).添加矩形
CGContextAddRect(ctx,CGRectMake(100,100, 200, 300));
24).设置填充实现
CGContextDrawPath(ctx,kCGPathFillStroke);
5.圆
25).初始化
CGContextRef ctx =UIGraphicsGetCurrentContext();
26).设置填充的颜色
CGContextSetRGBFillColor(ctx,0, 1,1, 1);
27).设置圆的位置和大小
CGContextAddEllipseInRect(ctx,CGRectMake(50,50, 100, 100));
28).填充的效果
CGContextDrawPath(ctx,kCGPathFillStroke);
6.扇形
29).初始化
CGContextRef ctx =UIGraphicsGetCurrentContext();
30).设置第一个上下文,第二个三个,表示扇形的圆心坐标,四表示半径,五和六表示扇形的起始角度和终止角度,参数7表示顺时针还是逆时针1是逆时针,
CGContextAddArc(ctx, 160, 200, 100,0, M_PI, 0);
31).设置填充的颜色
CGContextSetFillColorWithColor(ctx, [UIColorblackColor].CGColor);
32).设置填充的颜色
CGContextDrawPath(ctx,kCGPathFillStroke);
7.字
初始化
CGContextRef ctx =UIGraphicsGetCurrentContext();
33).设置背景的阴影颜色,第二个参数是阴影的偏移量,第三个参数是模糊度
CGContextSetShadowWithColor(ctx,CGSizeMake(10,10), 5,[UIColororangeColor].CGColor);
34).第一个参数,设置文字的位置,第二个参数是一个字典样式,用来设置字体的一些属性
[@"hello world"drawAtPoint:CGPointMake(50,100) withAttributes:@{NSForegroundColorAttributeName:[UIColorredColor]}];
8.贝塞尔曲线
添加一个二阶贝赛尔曲线
参数:
CGContextAddQuadCurveToPoint(ctx,200, 50, 300,150);
3阶:
初始化
CGContextRef ctx =UIGraphicsGetCurrentContext();
39).设置起始点
CGContextMoveToPoint(ctx,50, 200);
40).第二个参数,第三个四个五个六个参数是控制点,第六个和第七个参数表示的是终点的坐标
CGContextAddCurveToPoint(ctx,100, 1000, 200, -510,400, 200);
41).绘图
CGContextStrokePath(ctx);
二:
NSTimer:补充
1.初始化NSTimer*timer = [NSTimerscheduledTimerWithTimeInterval:1.0/60target:selfselector:@selector(timerAction)userInfo:nilrepeats:YES];
2.一个touch方法
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
只要调用这个方法,则对应的view会重新执行drawRect方法
[self.viewsetNeedsDisplay];
设置开始的时间
[timer setFireDate:[NSDate date]];
}
三:渐变
1.初始化
CGContextRef ctx =UIGraphicsGetCurrentContext();
2.参数一颜色空间
CGColorSpaceRef colorRef =CGColorSpaceCreateDeviceRGB();
3.设置渐变颜色数组
CGFloat components[] = {1,0,0,1,
0,1,0,1,
0,0,1,1,
};
4.位置
CGFloat locations[] = {0,0.5,1};
渐变属性,
参数一颜色空间.二是颜色.三、位置。四有效数量
CGGradientRef gtf = CGGradientCreateWithColorComponents(colorRef , components, locations, 3);
5.
线性渐变,第一个参数为上下文,第二个参数//最后一个参数取值0-3
CGContextDrawLinearGradient(ctx, gtf, CGPointMake(50, 50), CGPointMake(300, 300), 3);
6.
径向渐变,属性同上
CGContextDrawRadialGradient(ctx, gtf,CGPointMake(100,900), 100, CGPointMake(100,200), 150, 0);