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
相關文章
相關標籤/搜索