Quartz2D的学习2

  1 - (void)drawRect:(CGRect)rect {
  2     
  3     NSLog(@"drawRect");
  4     
  5     //获取图形的上下文
  6     CGContextRef context = UIGraphicsGetCurrentContext();
  7     //    CGContextMoveToPoint(context, 20, 100);
  8     //    CGContextAddLineToPoint(context, 200, 200);
  9     //    CGContextStrokePath(context);
 10     //  [self drawTriangle:context];
 11     //  [self drawRectWithContext:context];
 12     //  [self drawCircle:context];
 13     //  [self drawArc:context];
 14     //  [self drawCurve:context];
 15     //  [self drawEffect:context];
 16     //  [self drawFont:context];
 17     //  [self drawImage:context];
 18     //  [self clipImage:context];
 19  }
 20 
 21 
 22 //10。切割图片
 23 -(void)clipImage:(CGContextRef)context
 24 {
 25     //切成圆形的
 26     CGContextAddEllipseInRect(context, CGRectMake(20, 20, 50, 50));
 27     //切割操作
 28     CGContextClip(context);
 29     
 30     CGContextFillPath(context);
 31     
 32     UIImage *image = [UIImage imageNamed:@"account_candou"];
 33     
 34     [image drawAtPoint:CGPointMake(20, 20)];
 35     
 36     
 37 }
 38 
 39 //9.绘制图片
 40 -(void)drawImage:(CGContextRef)context
 41 {
 42     //绘图是有先后顺序关系的
 43     UIImage *image = [UIImage imageNamed:@"account_candou"];
 44     //[image drawAtPoint:CGPointMake(100, 100)];
 45     //可以放大或缩小
 46     //[image drawInRect:CGRectMake(100, 100, 200, 200)];
 47     //平铺图片
 48     [image drawAsPatternInRect:CGRectMake(100, 100, 200, 200)];
 49     
 50     
 51 }
 52 
 53 //8.绘制文字
 54 -(void)drawFont:(CGContextRef)context
 55 {
 56     
 57     NSString *str = @"老罗忘八端,不干人事";
 58     
 59     NSDictionary *dict = @{NSFontAttributeName:[UIFont systemFontOfSize:30],NSForegroundColorAttributeName:[UIColor orangeColor]};
 60     //1.调用字符串的drawAtPoint方法能够将文字直接绘制到view上
 61    // [str drawAtPoint:CGPointMake(10, 100) withAttributes:dict];
 62     //2.
 63     [str drawInRect:CGRectMake(10, 100, 300, 100) withAttributes:dict];
 64 }
 65 
 66 
 67 //7.画特效
 68 -(void)drawEffect:(CGContextRef)context
 69 {
 70     //矩形
 71     CGContextAddRect(context, CGRectMake(100, 100, 150, 150));
 72     //线宽
 73     CGContextSetLineWidth(context, 10);
 74     //线的颜色
 75     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
 76     //填充颜色
 77     CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
 78     //设置透明度
 79     CGContextSetAlpha(context, 0.5);
 80    
 81     /**
 82      *   设置阴影
 83      *
 84      *  @param context#> <#context#> 上下文
 85      *  @param offset#>  <#offset#> 相对于图的偏移
 86      *  @param blur#>    <#blur#>
 87      *
 88      *  @return <#return value description#>
 89      */
 90     CGContextSetShadow(context, CGSizeMake(20, 20), 10);
 91     
 92     //框的填充和内部填充同时显示
 93     CGContextDrawPath(context,kCGPathFillStroke);
 94     //CGContextStrokePath(context);
 95 }
 96 
 97 
 98 
 99 
100 //6.画曲线
101 -(void)drawCurve:(CGContextRef)context
102 {
103     //曲线起点
104     CGContextMoveToPoint(context, 20, 400);
105     /**
106      *  <#Description#>
107      *
108      *  @param c#>   <#c#> 上下文
109      *  @param cpx#> <#cpx#> 控制点的 x坐标
110      *  @param cpy#> <#cpy#> 控制点的 y坐标
111      *  @param x#>   <#x#> 终点的 x坐标
112      *  @param y#>   <#y#> 终点的 y坐标
113      *
114      *  @return 曲线
115      */
116    // CGContextAddQuadCurveToPoint(context, 160, 100, 300, 400);
117    // CGContextStrokePath(context);
118     //两个控制点的
119     CGContextAddCurveToPoint(context, 160, 100, 300, 400, 100, 200);
120     CGContextStrokePath(context);
121     
122 }
123 
124 //5.画扇形
125 -(void)drawArc:(CGContextRef)context
126 {
127     /**
128      *  <#Description#>
129      *
130      *  @param c#>          <#c#> 上下文
131      *  @param x#>          <#x#> 圆心的x坐标
132      *  @param y#>          <#y#> 圆心的y坐标
133      *  @param radius#>     <#radius#> 圆的半径
134      *  @param startAngle#> <#startAngle#> 开始的角度
135      *  @param endAngle#>   <#endAngle#> 结束的角度
136      *  @param clockwise#>  <#clockwise#> 方向(默认是顺时针0,1是逆时针)
137      * 角度是按顺时针算的
138      *  @return 一段弧
139      */
140 //    CGContextMoveToPoint(context, 100, 100);
141 //    CGContextAddArc(context, 100, 100, 50, 0, M_PI/3, 0);
142 //    //CGContextAddLineToPoint(context, 100, 100);
143 //    CGContextClosePath(context);
144 //    CGContextStrokePath(context);
145    // CGContextFillPath(context);
146     
147     CGContextMoveToPoint(context, 150, 150);
148     CGContextAddArc(context, 150, 150,100, 0, 270*M_PI/180, 1);
149     CGContextSetFillColorWithColor(context, [UIColor orangeColor].CGColor);
150     CGContextFillPath(context);
151     
152     CGContextMoveToPoint(context, 150, 150);
153     CGContextAddArc(context, 150, 150, 100, 0, 120*M_PI/180, 0);
154     CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor);
155     CGContextFillPath(context);
156     
157     CGContextMoveToPoint(context, 150, 150);
158     CGContextAddArc(context, 150, 150, 100, 120*M_PI/180, 270*M_PI/180, 0);
159     CGContextSetFillColorWithColor(context, [UIColor yellowColor].CGColor);
160     CGContextFillPath(context);
161     
162 }
163 
164 //4.画圆
165 -(void)drawCircle:(CGContextRef)context
166 {
167     CGContextAddEllipseInRect(context, CGRectMake(100, 100, 100, 100));
168     //空心渲染
169     //CGContextStrokePath(context);
170     //实心渲染
171     CGContextFillPath(context);
172 }
173 
174 
175 //3.画矩形
176 -(void)drawRectWithContext:(CGContextRef)context
177 {
178     CGContextAddRect(context, CGRectMake(20, 20, 100, 100));
179     //空心
180    // CGContextStrokePath(context);
181     //实心
182   //  CGContextFillPath(context);
183     //同时显示线框和填充
184     CGContextDrawPath(context, kCGPathFillStroke);
185     //以上三种渲染方式只能使用一种,如果都写了只显示先写的
186 }
187 
188 
189 //2.画三角形
190 -(void)drawTriangle:(CGContextRef)context
191 {
192     CGContextMoveToPoint(context, 20, 20);
193     CGContextAddLineToPoint(context, 100, 40);
194     CGContextAddLineToPoint(context, 150, 100);
195     //绘制空心的三角形
196    // CGContextAddLineToPoint(context, 20, 20);
197     CGContextClosePath(context);
198     CGContextSetStrokeColorWithColor(context, [UIColor orangeColor].CGColor);
199 
200    
201     //设置实心对应的颜色
202    // CGContextSetFillColorWithColor(context, [UIColor orangeColor].CGColor);
203     //绘制实心的
204   //  CGContextFillPath(context);
205     //渲染
206     CGContextStrokePath(context);
207 }
208 
209 //1.画线段
210 
211 -(void)drawLine:(CGContextRef)contextRef
212 {
213     //路径的设置
214     //给个起点
215     CGContextMoveToPoint(contextRef, 20, 100);
216     //给个线的终点
217     CGContextAddLineToPoint(contextRef, 200, 200);
218     
219     
220     //状态的设置
221     
222     //设置宽度
223     CGContextSetLineWidth(contextRef, 10);
224     
225     //设置颜色
226     //    CGContextSetRGBStrokeColor(contextRef, 1, 0, 1, 1);
227     CGContextSetStrokeColorWithColor(contextRef, [UIColor blueColor].CGColor);
228     
229     //线的风格(头尾圆角)
230     // CGContextSetLineCap(contextRef, kCGLineCapRound);
231     
232     
233     //画虚线
234     /**
235      *  <#Description#>
236      *
237      *  @param contextRef 作用域   在哪留一咕噜
238      *  @param phase#>    起点的左移量
239      *  @param lengths#>  规定实心和虚心的长度
240      *  @param count#>    实心和虚心的循环次数(count 必须等于lengths的长度)
241      *
242      *  @return 虚线
243      */
244     
245     CGFloat lengths[]= {10,10};//就是有色跟五色的长度
246     CGContextSetLineDash(contextRef, 0, lengths, 2);
247     
248     
249     
250     //画上View来(渲染)
251     CGContextStrokePath(contextRef);
252     
253     
254 }
View Code
原文地址:https://www.cnblogs.com/sdutmyj/p/4786966.html