Draw a line
//Get the current canvasCgcontextref CTX = Uigraphicsgetcurrentcontext ();//2. Stitching graphics (path) //Set segment widthCgcontextsetlinewidth (CTX,Ten);//Set style for end of segment headCgcontextsetlinecap (CTX, Kcglinecapround);//Set the style of the segment inflection pointCgcontextsetlinejoin (CTX, Kcglinejoinround);//Set colorCgcontextsetrgbstrokecolor (CTX,1,0,0,1);//Set a starting pointCgcontextmovetopoint (CTX,Ten,Ten);//Add a line segment to (+)Cgcontextaddlinetopoint (CTX, -, -);//Render onceCgcontextstrokepath (CTX);
Draw a triangle
// 1.获得画布 CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.画三角形 00); 100100); 15080); // 关闭路径(连接起点和最后一个点) CGContextClosePath(ctx); //设置颜色 0101); // 3.绘制图形 CGContextStrokePath(ctx);
Draw a rectangle
// 1.获得画布 CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.画矩形 CGContextAddRect(ctx, CGRectMake(1010150100)); // set : 同时设置为实心和空心颜色 // setStroke : 设置空心颜色 // setFill : 设置实心颜色 set];// CGContextSetRGBFillColor(ctx, 0, 0, 1, 1); // 3.绘制图形 CGContextFillPath(ctx);
Draw a Circle
// 1.获得画布 CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.画圆 CGContextAddEllipseInRect(ctx, CGRectMake(5010100100)); 10); // 3.显示所绘制的东西 CGContextStrokePath(ctx);
Drawing arcs
// 1.获得画布 CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.画圆弧 // x\y : 圆心 // radius : 半径 // startAngle : 开始角度 // endAngle : 结束角度 // clockwise : 圆弧的伸展方向(0:顺时针, 1:逆时针) 100100500); // 3.显示所绘制的东西 CGContextFillPath(ctx);
Painting pictures
UIImage *image = [UIImage imageNamed:@"me"]; // 2.画图片// [image drawAtPoint:CGPointMake(50, 50)];// [image drawInRect:CGRectMake(0, 0, 150, 150)]; [image drawAsPatternInRect:CGRectMake(00200200)];
Draw a String
//1. Get CanvasCgcontextref CTX = Uigraphicsgetcurrentcontext ();//2. Draw a rectangle CGRectCuberect = CGRectMake ( -, -, -, -); Cgcontextaddrect (CTX, cuberect);//3. Show what you are drawingCgcontextfillpath (CTX);//4. Drawing Text NSString*STR = @"haha good morning hello hi Hi hi";//[str drawatpoint:cgpointzero withattributes:nil]; nsmutabledictionary*attrs = [nsmutabledictionaryDictionary];//nsforegroundcolorattributename: Text color ///nsfontattributename: FontAttrs[nsforegroundcolorattributename] = [UicolorRedcolor]; Attrs[nsfontattributename] = [UifontSystemfontofsize: -]; [str drawinrect:cuberect withattributes:attrs];
Simple use of the "iOS development" quartz2d