标签:blog http color os 使用 io strong for ar
首先了解一下CGContextRef:
An opaque type that represents a Quartz 2D drawing environment.
Graphics Context是图形上下文,可以将其理解为一块画布,我们可以在上面进行绘画操作,绘制完成后,将画布放到我们的view中显示即可,view看作是一个画框.
自己学习时实现的demo,希望对大家有帮助,具体的实现看代码,并有完美的注释解释,还有一些对我帮助的博文供大家参考。都在代码里面。
看一下demo效果图先:
自定义CustomView类,CustomView.h:
- #import <UIKit/UIKit.h>
- #import <QuartzCore/QuartzCore.h>
- #define PI 3.14159265358979323846
- @interface CustomView : UIView
-
-
- @end
实现类CustomView.m:
- #import "CustomView.h"
-
- @implementation CustomView
-
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- }
- return self;
- }
-
-
- - (void)drawRect:(CGRect)rect
- {
-
-
- CGContextRef context = UIGraphicsGetCurrentContext();
-
-
- CGContextSetRGBFillColor (context, 1, 0, 0, 1.0);
- UIFont *font = [UIFont boldSystemFontOfSize:15.0];
- [@"画圆:" drawInRect:CGRectMake(10, 20, 80, 20) withFont:font];
- [@"画线及孤线:" drawInRect:CGRectMake(10, 80, 100, 20) withFont:font];
- [@"画矩形:" drawInRect:CGRectMake(10, 120, 80, 20) withFont:font];
- [@"画扇形和椭圆:" drawInRect:CGRectMake(10, 160, 110, 20) withFont:font];
- [@"画三角形:" drawInRect:CGRectMake(10, 220, 80, 20) withFont:font];
- [@"画圆角矩形:" drawInRect:CGRectMake(10, 260, 100, 20) withFont:font];
- [@"画贝塞尔曲线:" drawInRect:CGRectMake(10, 300, 100, 20) withFont:font];
- [@"图片:" drawInRect:CGRectMake(10, 340, 80, 20) withFont:font];
-
-
-
- CGContextSetRGBStrokeColor(context,1,1,1,1.0);
- CGContextSetLineWidth(context, 1.0);
-
-
- CGContextAddArc(context, 100, 20, 15, 0, 2*PI, 0);
- CGContextDrawPath(context, kCGPathStroke);
-
-
- CGContextAddArc(context, 150, 30, 30, 0, 2*PI, 0);
- CGContextDrawPath(context, kCGPathFill);
-
-
- UIColor*aColor = [UIColor colorWithRed:1 green:0.0 blue:0 alpha:1];
- CGContextSetFillColorWithColor(context, aColor.CGColor);
- CGContextSetLineWidth(context, 3.0);
- CGContextAddArc(context, 250, 40, 40, 0, 2*PI, 0);
-
- CGContextDrawPath(context, kCGPathFillStroke);
-
-
-
- CGPoint aPoints[2];
- aPoints[0] =CGPointMake(100, 80);
- aPoints[1] =CGPointMake(130, 80);
-
-
- CGContextAddLines(context, aPoints, 2);
- CGContextDrawPath(context, kCGPathStroke);
-
-
-
- CGContextSetRGBStrokeColor(context, 0, 0, 1, 1);
- CGContextMoveToPoint(context, 140, 80);
-
-
- CGContextAddArcToPoint(context, 148, 68, 156, 80, 10);
- CGContextStrokePath(context);
-
-
- CGContextMoveToPoint(context, 160, 80);
-
-
- CGContextAddArcToPoint(context, 168, 68, 176, 80, 10);
- CGContextStrokePath(context);
-
-
- CGContextMoveToPoint(context, 150, 90);
-
-
- CGContextAddArcToPoint(context, 158, 102, 166, 90, 10);
- CGContextStrokePath(context);
-
-
-
- CGContextStrokeRect(context,CGRectMake(100, 120, 10, 10));
- CGContextFillRect(context,CGRectMake(120, 120, 10, 10));
-
- CGContextSetLineWidth(context, 2.0);
- aColor = [UIColor blueColor];
- CGContextSetFillColorWithColor(context, aColor.CGColor);
- aColor = [UIColor yellowColor];
- CGContextSetStrokeColorWithColor(context, aColor.CGColor);
- CGContextAddRect(context,CGRectMake(140, 120, 60, 30));
- CGContextDrawPath(context, kCGPathFillStroke);
-
-
-
-
-
- CAGradientLayer *gradient1 = [CAGradientLayer layer];
- gradient1.frame = CGRectMake(240, 120, 60, 30);
- gradient1.colors = [NSArray arrayWithObjects:(id)[UIColor whiteColor].CGColor,
- (id)[UIColor grayColor].CGColor,
- (id)[UIColor blackColor].CGColor,
- (id)[UIColor yellowColor].CGColor,
- (id)[UIColor blueColor].CGColor,
- (id)[UIColor redColor].CGColor,
- (id)[UIColor greenColor].CGColor,
- (id)[UIColor orangeColor].CGColor,
- (id)[UIColor brownColor].CGColor,nil];
- [self.layer insertSublayer:gradient1 atIndex:0];
-
- CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
- CGFloat colors[] =
- {
- 1,1,1, 1.00,
- 1,1,0, 1.00,
- 1,0,0, 1.00,
- 1,0,1, 1.00,
- 0,1,1, 1.00,
- 0,1,0, 1.00,
- 0,0,1, 1.00,
- 0,0,0, 1.00,
- };
- CGGradientRef gradient = CGGradientCreateWithColorComponents
- (rgb, colors, NULL, sizeof(colors)/(sizeof(colors[0])*4));
- CGColorSpaceRelease(rgb);
-
-
-
- CGContextSaveGState(context);
- CGContextMoveToPoint(context, 220, 90);
- CGContextAddLineToPoint(context, 240, 90);
- CGContextAddLineToPoint(context, 240, 110);
- CGContextAddLineToPoint(context, 220, 110);
- CGContextClip(context);
-
-
- CGContextDrawLinearGradient(context, gradient,CGPointMake
- (220,90) ,CGPointMake(240,110),
- kCGGradientDrawsAfterEndLocation);
- CGContextRestoreGState(context);
-
-
- CGContextSaveGState(context);
- CGContextMoveToPoint(context, 260, 90);
- CGContextAddLineToPoint(context, 280, 90);
- CGContextAddLineToPoint(context, 280, 100);
- CGContextAddLineToPoint(context, 260, 100);
- CGContextClip(context);
-
- CGContextDrawLinearGradient(context, gradient,CGPointMake
- (260, 90) ,CGPointMake(260, 100),
- kCGGradientDrawsAfterEndLocation);
- CGContextRestoreGState(context);
-
-
- CGContextDrawRadialGradient(context, gradient, CGPointMake(300, 100), 0.0, CGPointMake(300, 100), 10, kCGGradientDrawsBeforeStartLocation);
-
-
-
- aColor = [UIColor colorWithRed:0 green:1 blue:1 alpha:1];
- CGContextSetFillColorWithColor(context, aColor.CGColor);
-
- CGContextMoveToPoint(context, 160, 180);
- CGContextAddArc(context, 160, 180, 30, -60 * PI / 180, -120 * PI / 180, 1);
- CGContextClosePath(context);
- CGContextDrawPath(context, kCGPathFillStroke);
-
-
- CGContextAddEllipseInRect(context, CGRectMake(160, 180, 20, 8));
- CGContextDrawPath(context, kCGPathFillStroke);
-
-
-
- CGPoint sPoints[3];
- sPoints[0] =CGPointMake(100, 220);
- sPoints[1] =CGPointMake(130, 220);
- sPoints[2] =CGPointMake(130, 160);
- CGContextAddLines(context, sPoints, 3);
- CGContextClosePath(context);
- CGContextDrawPath(context, kCGPathFillStroke);
-
-
- float fw = 180;
- float fh = 280;
-
- CGContextMoveToPoint(context, fw, fh-20);
- CGContextAddArcToPoint(context, fw, fh, fw-20, fh, 10);
- CGContextAddArcToPoint(context, 120, fh, 120, fh-20, 10);
- CGContextAddArcToPoint(context, 120, 250, fw-20, 250, 10);
- CGContextAddArcToPoint(context, fw, 250, fw, fh-20, 10);
- CGContextClosePath(context);
- CGContextDrawPath(context, kCGPathFillStroke);
-
-
-
- CGContextMoveToPoint(context, 120, 300);
- CGContextAddQuadCurveToPoint(context,190, 310, 120, 390);
- CGContextStrokePath(context);
-
- CGContextMoveToPoint(context, 200, 300);
- CGContextAddCurveToPoint(context,250, 280, 250, 400, 280, 300);
- CGContextStrokePath(context);
-
-
-
- UIImage *image = [UIImage imageNamed:@"apple.jpg"];
- [image drawInRect:CGRectMake(60, 340, 20, 20)];
- CGContextDrawImage(context, CGRectMake(100, 340, 20, 20), image.CGImage);
-
-
- }
-
-
- @end
IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角
标签:blog http color os 使用 io strong for ar
原文地址:http://www.cnblogs.com/yunis/p/3937503.html